2011-09-21 243 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