【发布时间】:2017-12-08 11:37:23
【问题描述】:
我正在尝试通过 angular.js 4 向 java rest api 发送请求。我收到以下错误:请求的资源上不存在“Access-control-allow-origin”标头。因此,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");
}
【问题讨论】:
标签: java angularjs spring-mvc