java请求的资源上不存在“Accesscontrolalloworigin”头。起源http://localhost:4200因此不允许访问
我正试图通过电子邮件发送请求。JS4到JavaRESTAPI。我收到以下错误:请求的资源上不存在“Access control allow origin”头。因此,不允许访问源http://localhost:4200 我的代码如下所示:
signupUser() {
// if (this.signupForm.dirty && this.signupForm.valid) {
// let headers = new Headers({ 'Content-Type': 'application/json' });
let data = {
"email": this.signupForm.value.email,
"password": this.signupForm.value.password,
'phone': this.signupForm.value.mobile,
// 'first_name': 'dd1',
// 'email': 'xyz@gmail.com',
// 'password': 'manju',
// 'phone': '123456',
'signup_type': 'Custom',
// social_media:
// "email": this.loginForm.value.name,
// "password": this.loginForm.value.password
};
console.log(data)
let options = {
type: "GET",
url: "http://localhost:8085/dashboard/signUp",
body: JSON.stringify(data)
};
this.http.post(options.url, options.body, {
headers: this.headers
}).subscribe((data) => {
console.log(data);
this.otpScreen = true;
}, (err) => {
console.log(err);
});
// alert(`Name: ${this.userForm.value.name} Email: ${this.userForm.value.password}`);
// }
}
@CrossOrigin(origins="http://localhost:4200")
在java代码中添加这一行必须解决这个问题,但仍然会遇到相同的错误。有人能告诉我如何解决这个问题吗?我的控制器看起来像:
@CrossOrigin
@RequestMapping("/signUp")
public void getOTP(HttpServletRequest request, HttpServletResponse response){
System.out.println("In Signup");
}
# 1 楼答案
只需在chrome浏览器中添加一个扩展名Allow Control Allow Origin:*1.0.3。 然后,地址栏旁边会出现一个CORS按钮,只需启用它并在chrome上运行您的项目,它就会正常工作。 Link for Allow-Control-Allow-Origin extension
# 2 楼答案
或者向spring应用程序添加一个全局配置,允许localhost:4200
例如