【发布时间】:2018-09-22 21:27:50
【问题描述】:
当我向服务器发送 POST 请求时出现错误:
Failed to load http://localhost:8181/test: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 403.
后端是用 Java Spring 编写的。 我创建测试的方法:
createTest() {
const body = JSON.stringify({
'description': 'grtogjoritjhio',
'passingTime': 30,
'title': 'hoijyhoit'
});
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Accept': 'application/json'
}
)
};
return this._http.post(`${this._config.API_URLS.test}`, body, httpOptions)
.subscribe(res => {
console.log(res );
}, error => {
console.log(error);
});
}
Get 方法有效,但 Post 无效。他们都在 Swagger 和 Postman 工作。我多次更改 POST 方法。我的代码中的标头不起作用,但我解决了将它们扩展到 Google Chrome 的问题。只有一个错误:
Response for preflight has invalid HTTP status code 403.
在我看来,这不是 Angular 问题。请告诉我我或我的朋友(编写后端)如何解决这个问题。
【问题讨论】:
-
您能否在此处分享 chrome 网络选项卡的完整详细信息,因为这将有助于查看是否有合适的标头出现?
标签: java spring cors angular5 http-status-code-403