开发者

jquery, problem with ajax call with dataType is json

After upgrading to jQuery 1.5.2 I star开发者_JS百科t to have problems with my ajax calls when returning json data.

The error is (returned by templateGet() below):

Ajax call failed: [object Object] parsererror jQuery152040843801534161517_1302269320612 was not called

Heres a sample return json:

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

And heres the jQuery function

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();
        }
    });
}

Anyone can see what I do wrong?


Are you using the validation plugin? If so, make sure you get a new copy that's compat with 1.5 - this is a known issue and one that I have had as well.

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


You first need to parse the returned JSON value....

You cannot use data.subject right away

First you need to download the json2.js file and add to your app..

then parse the variable data

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

Then use variable response instead of data in the code you posted

success: function(data) {
            var response=eval("("+JSON.stringify(data)+")");
            $("#preview_subject").empty().html(response.subject);
            $("#preview_body").empty().html(response.body);
}


In the jquery1.5.2.js find the line:

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

and change to

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

That works, and all of my $.ajax functions are happy again.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜