【发布时间】:2012-09-02 09:40:49
【问题描述】:
我有一个关于XMLHttpRequest 对象如何工作的问题。在https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest 的属性下,我正在查看“响应”属性。描述说如果我的内容类型是 json,我应该得到一个 JavaScript 对象。但是,在我的代码中,xhr.response 属性是 undefined。
具体来说,我有一个在 DOM 元素上设置的 .ajaxSuccess() 回调,它接收参数 (event, xhr, settings)。 xhr.responseText 是我的 json 的明文版本,但 xhr.response 未定义。有什么方法可以在不调用$.parseJson(xhr.responseText) 的情况下从 xhr 获取对象?
【问题讨论】:
-
检查服务器是否返回“application/json”内容类型。这可能是 jquery 无法自动解析 json 的原因。
-
嗯,是的,我的 xhr.responseType 也未定义。但在我的服务器代码(web.py 框架)中,我确实调用了 web.header("Content-Type", "application/json")。你能想到 xhr 没有得到它的任何原因吗?
标签: jquery ajax json xmlhttprequest