2013-05-21 38 views
11

的jQuery(async ajax section)的官方文檔工作說:CORS同步請求不能在Firefox

跨域請求和數據類型:「JSONP」請求不支持 同步操作。

然而這部作品在最近所有的瀏覽器但Firefox版本> = 20.這是呼叫的類型我正在做:

$.ajax({ 
     type : "GET", 
     async: false, 
     dataType : "text", 
     url : link, 
     xhrFields: { withCredentials: true }, 

     success: function(response){ 
     console.log("success "); 
     }, 
     error: function(error){ 
      console.error(error);    
     } 
}); 

有沒有人有一個線索,爲什麼發生這種情況?

UPDATE: 伊夫測試都與jQuery和香草XHR誤差總是相同

[異常...「的參數或操作不被 底層對象支持」代碼「15」 nsresult: 「0x8053000f (InvalidAccessError)」

+1

您沒有使用dataType:「jsonp」,所以它必須是跨域的一部分,但是您沒有向我們展示從何處發出請求的位置。 – tandrewnichols

+0

FF中會發生什麼?網絡監察員告訴你什麼? – Bergi

+0

@tandrewnichols使用CORS進行跨域請求。 – mfreitas

回答

18

使用beforeSend,而不是xhrField

$.ajax({ 
    type : "GET", 
    async: false, 
    dataType : "text", 
    url : link, 
    beforeSend: function(xhr) { 
     xhr.withCredentials = true; 
    }, 
    success: function(response){ 
     console.log("success "); 
    }, 
    error: function(error){ 
     console.error(error);    
    } 
});