我只是尝试从我的http://localhost:8080
使用mocky.io
但出现此错误:
XMLHttpRequest cannot load http://www.mocky.io/v2/5715f13a1100004d1187d9e1. 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:8080' is therefore not allowed access.
我的请求看起来像这样:
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Access-Control-Allow-Origin', '*');
this.people = http.get('http://www.mocky.io/v2/5715f13a1100004d1187d9e1', { headers: headers })
.map(response => response.json());
最佳答案
实际上你应该在mocky.io中设置 header Access-Control-Allow-Origin
。
只需单击“切换到高级模式”,您就会看到“自定义标题”输入。添加 Access-Control-Allow-Origin
并将 *
作为其值。然后创建您的mocky.io url。现在应该可以工作了。
关于http-headers - 请求未通过访问控制检查 : No 'Access-Control-Allow-Origin' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36714906/