我正在使用jquery来访问一个使用ajax的外部api。
$.ajax({
url: https://blahblah,
method: "GET",
contentType: "text/plain",
dataType: "json",
crossDomain: true,
accept: "application/json",
async: true,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Headers": "Origin, Content-Type, Accept",
success: function (data) { //more code}IE中的所有功能都运行得很好,但由于Chrome的同源策略,它不能正常工作。这是我在控制台中得到的错误
XMLHttpRequest cannot load https://blahblah.
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:8889' is therefore not allowed access.
localhost/ Failed to load resource: the server responded with a status of 403 (Forbidden)标题如下所示
Response Headers
HTTP/1.1 200 OK
Date: Fri, 23 Jun 2017 15:05:44 GMT
Server: xxxxx
X-Frame-Options: SAMEORIGIN
Content-Type: application/json
Connection: close
Transfer-Encoding: chunked
Request Headers
view source
Accept:*/*
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:access-control-allow-headers,access-control-allow-methods,access-control-allow-origin,x-frame-options
Access-Control-Request-Method:GET
Connection:keep-alive
Host:blahblah
Origin:http://localhost:8889因为这是一个.net MVC4 IIS7.5应用程序,所以我尝试在WEB.CONFIG文件中设置自定义头,但也不起作用
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*"/>
<add name="Access-Control-Allow-Methods" value="GET"/>
<add name="Access-Control-Allow-Headers" value="*"/>
</customHeaders>
</httpProtocol>请帮帮忙
https://stackoverflow.com/questions/44724775
复制相似问题