使用Spring Boot Rest Api的Angular4中的Access-Control-Allow-Origin错误
问题描述:
这是service.ts代码。我使用服务器端REST API来验证 用户名和密码。即使CORS出现在服务器端 之后,错误仍然存在。如果有人知道我在做什么worng帮助我和 在此先感谢。使用Spring Boot Rest Api的Angular4中的Access-Control-Allow-Origin错误
import { Injectable } from '@angular/core';
import { Http, Headers, Response } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
@Injectable()
export class Service {
constructor(private http: Http) { }
authenticate(model) {
var creds = "name=" + model.email + "&password=" + model.password;
console.log(creds)
var headers = new Headers();
headers.append('Content-Type', 'application/json');
return new Promise(resolve => {
this.http.post('http://sample.sample.com/api/authenticate', creds, {headers: headers}).subscribe(data => {
if(data.json().success){
resolve(true);
}
else
resolve(false);
});
});
}
}
答
var creds = {"name":model.email,"password" : model.password};
您需要通过这样的数据。
this.http.post('http://sample.sample.com/api/authenticate', JSON.stringify(creds), {headers: headers}).subscribe(data => {
+0
谢谢你的回答对我来说工作正常 –
答
您需要在服务器上启用CORS /在您的API中,CORS未启用时会发生此错误。
向我们展示'服务器端' –