jQuery将无法解析来自AJAX查询我的JSON [英] jQuery won't parse my JSON from AJAX query
问题描述
我有困难解析使用jQuery.ajax从我的服务器返回一些JSON数据()
I'm having difficulty parsing some JSON data returned from my server using jQuery.ajax()
要执行我使用AJAX:
To perform the AJAX I'm using:
$.ajax({
url: myUrl,
cache: false,
dataType: "json",
success: function(data){
...
},
error: function(e, xhr){
...
}
});
如果我回到一个项目数组然后正常工作:
And if I return an array of items then it works fine:
[ { title: "One", key: "1" }, { title: "Two", key: "2" } ]
成功函数被调用,并接收到正确的对象。
The success function is called and receives the correct object.
然而,当我试图返回一个对象:
However, when I'm trying to return a single object:
{ title: "One", key: "1" }
误差函数被调用,XHR包含'parsererror。我试着发送下来的电线包裹之前在括号中的服务器上的JSON,但它没有什么区别。然而,如果我粘贴的内容转换为字符串在Javascript中,然后使用eval()函数,它完美地计算它。
The error function is called and xhr contains 'parsererror'. I've tried wrapping the JSON in parenthesis on the server before sending it down the wire, but it makes no difference. Yet if I paste the content into a string in Javascript and then use the eval() function, it evaluates it perfectly.
任何想法,我做错了什么?
Any ideas what I'm doing wrong?
安东尼
推荐答案
是您的服务器发送数据的Content-Type * / json的
?如果没有,修改相应的响应头。发送应用/ JSON的
就可以了,例如。
Is your server sending data as Content-Type "*/json"
? If not, modify the response headers accordingly. Sending "application/json"
would be fine, for example.
这篇关于jQuery将无法解析来自AJAX查询我的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!