2017-06-23 25 views
0

我正在使用jQuery打jQuery的外部api。鉻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 

的Referer:http://localhost:8889/

因爲這是一個.NET MVC4 IIS 7.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> 

Plea SE幫助

回答

0

我很遺憾地告訴你,這是不可能做到的JavaScript AJAX不先安裝CORS插件,可以讓你做這樣的插件,這樣

例子:Allow-Control-Allow-Origin

+0

這是荒謬的。必須有一個解決方法。你不能告訴你的所有用戶安裝一個chrome插件來使用你的應用程序 – Wil