【发布时间】:2019-10-31 21:09:37
【问题描述】:
跨源未发生,被 CORS 政策阻止。
我在 Windows 系统上。
在-.service.ts 文件中:
makeHeaders() {
const headers = new HttpHeaders({
'content':"application/json",
'Content-Type': 'application/x-www-form-urlencoded',
// 'Access-Control-Allow-Credentials': 'true',
'Access-Control-Allow-Origin': 'http://localhost:4200',
'Access-Control-Allow-Methods': 'OPTIONS, GET, POST',
'Access-Control-Allow-Headers': 'Origin, Content-Type, Accept, Access-Control-Allow-Origin, Authorization, X-Requested-With'
})
return headers;
}
在proxy.conf.json 文件中:
"target": "http://localhost:1111"
我在浏览器控制台中收到此错误:
从源“http://localhost:4200”对“http://localhost:1111/api/v1/employees/create”处的 XMLHttpRequest 的访问已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:没有“Access-Control-Allow-Origin”标头出现在请求的资源上。 core.js:15724 ERROR HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "Unknown Error", url: "http://localhost:1111/api/v1/employees/create", ok: false, ...}
谢谢
【问题讨论】:
-
cors 问题无法从客户端应用程序修复。我可以看到您正在使用代理,但它没有正确设置。