2014-10-27 132 views
0

我想使用JSONP請求另一個域上的飼料。我知道內容類型應該是JSON或JavaScript,但是它是text/plain,並且我無法控制服務器,因此我無法更改標題。我如何獲得AJAX呼叫的工作?請求JSONP與文本/普通的MIME類型

這裏是我迄今爲止 -

function asdf() { 
    $.ajax({ 
     url: "http://example.com/path/to/sharepoint/_vti_bin/listdata.svc/TestCalendar/$count", 
     jsonp: "callback", 
     dataType: "jsonp", 
     contentType: "text/plain", 

     // work with the response 
     success: function(response) { 
      console.log(response); // server response 
     } 
    }); 
} 

如果我只是嘗試有規律的要求,很明顯,我只是得到一個CORS錯誤。

XMLHttpRequest無法加載http://example.com/path/to/sharepoint/_vti_bin/listdata.svc/TestCalendar/ $ count。請求的資源上沒有「Access-Control-Allow-Origin」標題。因此不允許訪問原產地'http://example.com'。響應有HTTP狀態代碼401

回答

0

嘗試直接執行你在瀏覽器的網址,以確保所有工作正常
之後,嘗試與這個jQuery配置:

$.ajax({ 
     url  : "YOUR_URL", 
     dataType : 'jsonp', 
     crossDomain: true, 
     type  : 'POST', 
     timeout : 30000, // in milli seconds 
     cache  : false 
     success:function(response) { 
      console.log(response); 
     } 
    });