2015-09-11 47 views
-1

所有, 我有一個在IE(我知道對不對?),但工作正常下AJAX請求時,我嘗試使用Chrome瀏覽器相同的代碼,我得到了以下錯誤:鉻阻擋Ajax請求

XMLHttpRequest cannot load http://ahmwpsds01:1234/NASAService/GetNASAUser.asmx/GetUserByName . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://ahmwpsds01 ' is therefore not allowed access. The response had HTTP status code 500.

Ajax請求

function getNASAProfile(user) { 
    var dfd = new $.Deferred(function() { 
     $.support.cors = true; 
     $.ajax({ 
      type: "POST", 
      url: "http://ahmwpsds01:1234/NASAService/GetNASAUser.asmx/GetUserByName", 
      data: "{userName: '" + UserProfile.Name + "'}", 
      dataType: "json", 
      contentType: "application/json; charset=utf-8", 
      accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", 
     }) 
     .done(function (data) { 
      //alert('Call Succeeded'); 
      dfd.resolve(data.d); 
     }) 
     .fail(function (jqXHR, textStatus, errorThrown) { 
      alert('Call Failed\n\n' + jqXHR.statusText + "\n\n" + errorThrown + "\n\n" + textStatus); 
     }); 
    }); 
    return dfd.promise(); 
} 

有什麼想法?

+0

是啊,典型的CORS錯誤。 IE不正確地允許它,或者不發送給定請求應該要求的OPTIONS預檢請求。 –

+0

這就是它說的。您正在請求的網址需要一個Access-Control-Allow-Origin標題。 https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Access-Control-Allow-Origin – danielrsmith

+0

不,我不這麼認爲,因爲我發送$ .support.cors,它的工作原理IE瀏覽器。 –

回答

0

編輯:沒有注意到你想POST請求。您應該考慮針對您的情況使用後端方法

您試圖訪問位於不同服務器上的資源。出於安全原因,您不能通過簡單的AJAX調用來完成此操作。

要解決該問題,可以使用JSONP。實現它的簡單方法是將?callback=?添加到URL中。另外,服務器響應必須封裝在函數定義中。您可能需要使用YQL之類的代理將響應轉換爲JSONP。此鏈接

的更多信息: http://json-jsonp-tutorial.craic.com/index.html

+0

然後向他展示解決方案... –

+0

我編輯了我的答案,對不起,第一次沒有提供足夠的細節 –

+0

請記住,使用jsonp數據類型進行POST是不可能的**。 –