如何使用jQuery ajax调用的“内容”字段来处理响应?

时间:2022-11-29 14:03:03

I have a problem with an ajax request to a site.

对于一个站点的ajax请求,我有一个问题。

It returns as a response a json object and I cannot handle it with a success:function(data) like usual. I get a

它以一个json对象的形式返回,而我无法成功地处理它:函数(数据)和往常一样。我得到一个

Uncaught SyntaxError: Unexpected token :

未捕获的SyntaxError:意外令牌:

error on chrome, and

在chrome错误,

SyntaxError: invalid label {"response":{

{“响应”:{ SyntaxError:无效的标签

on firefox...

firefox…

the response is :

的反应是:

{"response":{
  "header": {
    "found": 6,
    "query": "something"
  },
  "results": [
    { ... }
  ]
}

I thought of using the contents field of the ajax() function to wrap the response somehow, but there is no example to see how it's used. Any other ideas to handle the request?

我想过使用ajax()函数的内容字段来包装响应,但是没有示例可以看到它是如何使用的。还有其他想法来处理这个请求吗?

this is the call I make:

这是我的电话:

$.ajax({
     url:'http://somesite.com/search/query',
     dataType: 'jsonp',
     data: { q: 'something' },
     success:function(json){
         // debugger;
         alert("Success");
     },
     error:function(err, data){
         alert("Error");
     },
});

From the jQuery website http://api.jquery.com/jQuery.ajax/

来自jQuery网站http://api.jquery.com/jQuery.ajax/。

contents Type: PlainObject An object of string/regular-expression pairs that determine how jQuery will parse the response, given its content type. (version added: 1.5)

内容类型:一个字符串/正则表达式对的对象,它决定了jQuery如何解析响应,给出了它的内容类型。(版本补充道:1.5)

2 个解决方案

#1


0  

The reason you're getting an error is because it's not valid JSONP, but just regular JSON missing the callback, and there's nothing you can do about that, other than getting the content on the serverside or having the external service change their API, as JSONP is the only datatype supported for cross domain requests, unless CORS is enabled.

你得到一个错误的原因是因为它不是有效的JSONP,只是普通的JSON失踪的回调,没什么可以做,除了服务端上的内容或外部服务改变他们的API,如JSONP是唯一的数据类型支持跨域请求,除非启用歌珥。

#2


0  

Yahoo Query Language is another option for you.

雅虎查询语言是你的另一个选择。

Links:

链接:

http://yuiblog.com/sandbox/yui/3.2.0pr1/examples/io/io-xdr.html

http://yuiblog.com/sandbox/yui/3.2.0pr1/examples/io/io-xdr.html

http://staff.washington.edu/ta*s/343INFO/yqlJSON.htm

http://staff.washington.edu/ta*s/343INFO/yqlJSON.htm

#1


0  

The reason you're getting an error is because it's not valid JSONP, but just regular JSON missing the callback, and there's nothing you can do about that, other than getting the content on the serverside or having the external service change their API, as JSONP is the only datatype supported for cross domain requests, unless CORS is enabled.

你得到一个错误的原因是因为它不是有效的JSONP,只是普通的JSON失踪的回调,没什么可以做,除了服务端上的内容或外部服务改变他们的API,如JSONP是唯一的数据类型支持跨域请求,除非启用歌珥。

#2


0  

Yahoo Query Language is another option for you.

雅虎查询语言是你的另一个选择。

Links:

链接:

http://yuiblog.com/sandbox/yui/3.2.0pr1/examples/io/io-xdr.html

http://yuiblog.com/sandbox/yui/3.2.0pr1/examples/io/io-xdr.html

http://staff.washington.edu/ta*s/343INFO/yqlJSON.htm

http://staff.washington.edu/ta*s/343INFO/yqlJSON.htm