2011-09-21 246 views
0

我發送GET請求(它返回JSON)。代碼看起來像:GET ajax請求

$.ajax({ 
    url: "http://www.new.marketprice.ru/retrieveRegions.html", 
    dataType: "jsonp", 
    data: { 
    searchStr: request.term 
    }, 
    error: function() { 
    console.log('epic fail'); 
    }, 
    success: function(data) { 
    console.log(data); 
    } 
}); 

它返回(到控制檯);

Resource interpreted as Other but transferred with MIME type undefined. 
epic fail 

Network標籤我看到返回的數據GET請求:

[ 
    { "region":"Московская область","countryId":1, 
    "cityId":23,"regionId":12345,"city":"Москва","country":"Россия"}, 
    {"region":"Ленинградская область","countryId":1,"cityId":453, 
     "regionId":54321,"city":"Санкт Петербург","country":"Россия"} 
] 

爲什麼error回調叫什麼名字?


UPD

好吧,我設置json現在沒有警告,但錯誤:

XMLHttpRequest cannot load http://www.new.marketprice.ru/retrieveRegions.html?searchStr=test. Origin http://new.marketprice.ru is not allowed by Access-Control-Allow-Origin 

它是如此奇怪,因爲運行腳本位於同一個域中:(

+0

是在該請求頁面不同域的網址試試? (json vs jsonp dataType。) – BNL

+0

如果它位於同一個域中,請用'「/retrieveRegions.html」' – Blazemonger

+0

@ mblase75替換'「http://www.new.marketprice.ru/retrieveRegions.html」'非常感謝!它現在有效。 – Ockonal

回答

2

它只是一個json響應而不是JSONP。一般來說,對於JSONP,請求會有回調方法並且響應將被打包在該功能名稱

1

您的標題沒有指定正確的MIME類型(application/json-p)和jQuery(?)混淆,因爲它不確定它是否應該將其處理爲json或不。

看看你是否可以讓你的服務器用正確的類型進行響應。適當的標題被稱爲Content-Type。

編輯:哎呀,和OP是正確的,它甚至沒有JSON-P。嘗試改變jquery預期的只是'json'。

0

我也是在類似這樣的問題,只需使用一個以上參數callback=?json=jsonp