2

升级到 jQuery 1.5.2 后,在返回 json 数据时,我的 ajax 调用开始出现问题。

错误是(由下面的 templateGet() 返回):

Ajax 调用失败:[object Object] parsererror jQuery152040843801534161517_1302269320612 未被调用

下面是一个示例返回 json:

{"subject":"Test subject","body":"Test body"}

继承人的jQuery函数

function ajax_templateGet(templateid) {
    showLoading();
    var query = '?action=get_template' + '&templateid=' + templateid;
    $.ajax({
        type: 'POST',
        url: 'script/ajax/mail_template/mail_template.ashx' + query,
        data: '',
        dataType: 'json',
        success: function(data) {
            $("#preview_subject").empty().html(data.subject);
            $("#preview_body").empty().html(data.body);
        },
        error: function(xhr, status, error) {
            $.jGrowl($.i18n._('Ajax call failed: ' + xhr + ' ' + status + " " + error), { header: $.i18n._('Ajax call failed!') });
        },
        complete: function(jqXHR, textStatus) {
            hideLoading();
        }
    });
}

任何人都可以看到我做错了什么?

4

3 回答 3

3

你在使用验证插件吗?如果是这样,请确保您获得与 1.5 兼容的新副本 - 这是一个已知问题,我也遇到过。

https://github.com/jzaefferer/jquery-validation

于 2011-04-08T13:47:42.493 回答
0

您首先需要解析返回的 JSON 值....

你不能data.subject马上使用

首先,您需要下载json2.js文件并添加到您的应用程序中。

然后解析变量data

var response=eval("("+JSON.stringify(data)+")");

然后在您发布的代码中使用变量response而不是data

success: function(data) {
            var response=eval("("+JSON.stringify(data)+")");
            $("#preview_subject").empty().html(response.subject);
            $("#preview_body").empty().html(response.body);
}
于 2011-04-08T13:45:37.663 回答
0

在 jquery1.5.2.js 中找到以下行:

d.ajaxPrefilter("json jsonp", function (b, c, e)

并更改为

d.ajaxPrefilter("jsonp", function (b, c, e)

这行得通,我的所有 $.ajax 函数都再次感到高兴。


来源:: http://debeerdev.wordpress.com/2011/04/13/jquery-1-5-2-json/

于 2011-04-18T16:07:58.957 回答