jQuery won#39;t parse my JSON from AJAX query(jQuery 不会从 AJAX 查询中解析我的 JSON)
问题描述
我在使用 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"
形式发送数据?如果不是,请相应地修改响应标头.例如,发送 "application/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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:jQuery 不会从 AJAX 查询中解析我的 JSON


- 如何显示带有换行符的文本标签? 2022-01-01
- 从原点悬停时触发 translateY() 2022-01-01
- 使用 iframe URL 的 jQuery UI 对话框 2022-01-01
- 如何向 ipc 渲染器发送添加回调 2022-01-01
- 在不使用循环的情况下查找数字数组中的一项 2022-01-01
- 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01
- 如何调试 CSS/Javascript 悬停问题 2022-01-01
- 为什么悬停在委托事件处理程序中不起作用? 2022-01-01
- 为什么我的页面无法在 Github 上加载? 2022-01-01
- 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01