我有一个与 CORS 和所需标头相关的问题。
Setup我运行一个 Angular 项目和 Angular-cli (localhost:4200),我想通过 HTTP 请求从 WebService 访问 JSON。当我直接在浏览器中使用 URL 时,一切都很好并且 JSON 会显示。一旦我从我的角度应用程序发送请求,我就会收到以下错误:
XMLHttpRequest cannot load http://t00-holcim:8888/deliveries?fields=liefer_nr,dispo_nr,lieferscheinsta…r,empfaenger_nr,bestellinfo1,bemerkung&limit=3&deliveryNoteDate=2017-04-27. 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.
这是程序代码:
.......
const headers = new Headers();
headers.append('Content-Type', 'application/json; charset=utf-8'); headers.append('Access-Control-Allow-Origin', '*');
const options = new RequestOptions({ method: RequestMethod.Post, headers: headers });
this.http.get(this.buildURLString(), options).map(this.extractData)
.subscribe((data) => {........}
我上网查了一下,发现这个错误与CORS有关。因此,我附加到标头 headers.append('Access-Control-Allow-Origin', '*');但这没有帮助。
我获得了 Chrome 扩展 CORS 并启用“启用跨源资源共享”但这没有帮助。
这是服务器端还是客户端的配置更改。