我不知道为什么我的angular-cli代理配置错误。 我仍然从chrome中收到CORS错误。
这是代理配置:
"/sites": {
"target": "http://dati.venezia.it",
"secure": false,
"logLevel": "debug",
"changeOrigin": true,
"pathRewrite": {
"^/sites": ""
}
}
这是组件发出的请求
ngOnInit() {
this.getData()
}
getBaseUrl() {
return 'http://dati.venezia.it/';
}
getData(){
const headers = new HttpHeaders({ 'Content-Type': 'text/plain' });
this.http.get(this.getBaseUrl() + 'sites/default/files/dataset/opendata/temparia.json', { responseType: 'text', headers }).subscribe(
data => {
console.log('getData OK -> ', data);
},
error => {
console.log('getData ERROR -> ', error)
}
)
}
[HPM] Proxy rewrite rule created: "^/sites" ~> ""
[HPM] Subscribed to http-proxy events: [ 'error', 'close' ]
我仍然收到CORS错误,但我不明白为什么,有人可以帮忙吗?
Access to XMLHttpRequest at 'http://dati.venezia.it/sites/default/files/dataset/opendata/temparia.json' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
答案 0 :(得分:1)
在服务http://dati.venezia.it的服务器中,您需要设置标题:
Access-Control-Allow-Origin: '*'
如果您无权访问服务器,请禁用Chrome Web安全:
在Mac OSX中,使用以下命令运行chrome:
open -n -a /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --args --user-data-dir="/tmp/chrome_dev_test" --disable-web-security
在Windows 10上,命令为:
C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --disable-web-security --disable-gpu --user-data-dir=~/chromeTemp