简体   繁体   English

获取ajax请求

[英]GET ajax request

I'm sending GET request (which returns JSON). 我正在发送GET请求(返回JSON)。 The code looks like: 代码如下:

$.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);
  }
});

It returns (into console); 它返回(进入控制台);

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

But in Network tab I see GET request with returned data: 但是在“ Network选项卡中,我看到带有返回数据的GET请求:

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

Why does error callback is called? 为什么调用error回调?


UPD UPD

Okay, I set json and now no warning but error: 好的,我设置了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

It's so strange because running script is located at same domain :( 太奇怪了,因为运行脚本位于同一域中:(

It is only a json response not JSONP. 它只是一个JSON响应,而不是JSONP。 Generally for JSONP the request will have callback method and response will be wrapped in that function name 通常对于JSONP,请求将具有回调方法,响应将包装在该函数名称中

Your headers don't specify the correct mime-type ( application/json-p ) and jQuery(?) is confused because it's not sure if it should process it as json or not. 您的标头未指定正确的mime类型( application/json-p ),并且jQuery(?)感到困惑,因为不确定是否应将其作为json处理。

See if you can get your server to respond with the correct type. 查看您是否可以使服务器以正确的类型进行响应。 The appropriate header is called Content-Type. 适当的头称为Content-Type。

EDIT: whoops, and the OP is right, its not even JSON-P. 编辑:糟糕,OP是正确的,它甚至不是JSON-P。 Try changing what jquery is expecing to just 'json' first. 尝试先将jquery的期望值更改为“ json”。

i was also in problem like this just try by using one more argument callback=? 我也遇到这样的问题,只需尝试使用另一个参数callback=? or json=jsonp json=jsonp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM