对于angular-cli,localhost:4200" no access-Control-Allow-Origin [英] No Access-Control-Allow-Origin for angular-cli "localhost:4200"
问题描述
我遇到了与CORS和所需标题相关的问题。
I have a problem related to CORS and the needed headers.
设置
我运行角度项目和角度 - cli(localhost:4200)我想通过HTTP服务从WebService访问一个JSON。当我在浏览器中直接使用URL时,一切都很好,并显示JSON。一旦我从angular-app发送请求,我就会收到以下错误:
Setup I run a angular project und angular-cli (localhost:4200) I want to access a via a HTTP-Request a JSON from a WebService. When I using the URL direct in the browser everything is fine and the JSON gets displayed. As soon as I send the request from my angular-app I get the following error:
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有关。因此我追加到标题header.append('Access-Control-Allow-Origin','*'); 但这没有帮助。
I searched the net and I found that the error is related to CORS. Therefore I append to the header headers.append('Access-Control-Allow-Origin', '*'); but this does not help.
我获得了Chrome的扩展CORS并启用了启用跨源资源共享 ,但它没有帮助。
I got extension CORS for Chrome and enable "Enable cross origin resource sharing" but it didn't help.
这是服务器端还是客户端的配置更改。
Is this a configuration change on the server side or client side.
推荐答案
我们找到了解决方案。当我们使用SpringBoot的RESTFul-Server时,我们添加了' Access-Control-Allow-Origin','*'
它工作..
我一直在想,因为它说预检这需要在客户端而不是服务器端解决。
希望这个帮助任何人。
We found the solution. As we are using the RESTFul-Server of SpringBoot, to this server we added the "'Access-Control-Allow-Origin', '*'
" and it worked..
I was always thinking because it says "preflight" that this need to be solved on the client side and not on the server side.
Hope this helps anybody.
这篇关于对于angular-cli,localhost:4200" no access-Control-Allow-Origin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!