0

我正在使用 jQuery 1.8,并且我有一个返回 JSON 的 ajax 调用。如果有错误,它只会返回{ "status": "there was an error" },否则它会返回一个文档,该文档是 ajax 请求意味着要加载的数据,看起来像{ "document": { ... } }

在 Firefox 和 Chrome 上,以下代码有效,但在 IE8 上,我收到一条错误消息data.status is null or not an object(当请求的 URL 明确返回文档而不仅仅是状态时),这会导致脚本崩溃。有人知道如何绕过 IE8 上的此错误消息吗?

$.ajax({
    url: "GanttLoader.ashx?action=loadGantt&gantt=" + current_selected_gantt + "&userId=" + userId,
    context: document.body,
    type: "GET",
    dataType: "json",
    success: function (data) {
        if (data.status != null) {
            if (data.status == "none") {
                alert("no gantts found when attempted to load");
            } else if (data.status == "locked") {
                alert("this gantt is locked");
            }
        } else if (data.document != null) {
            /* process the gantt */
        }
    },
    error: function () {
        alert("couldn't load gantt charts");
    }
});
4

3 回答 3

0

确保您的响应的 Content-Type 是 application/json

于 2012-09-10T15:01:54.450 回答
0

会不会是 data.status 不是 null,而是“未定义”?也许用 undefined 测试 data.status 会做正确的工作。

if (data.status != undefined)
{
...
}
于 2012-09-10T15:10:02.577 回答
0

JSON 响应实际上没有发送任何内容,因为在 IE8userId中为空,破坏了我的 .ashx 处理程序。因为我的 cookie 解析代码(未显示)在 Chrome / FF 上有效,但在 IE 上无效,所以我认为 URL 输入正确。我搬到了 jquery cookie 插件,问题得到了解决(为什么 cookie 解析和设置在 jQuery 中不是标准的?)。感谢您的所有帮助,即使这是我的错误。

于 2012-09-10T17:43:49.560 回答