Muleskinner
Muleskinner

Reputation: 14468

jquery, problem with ajax call with dataType is json

After upgrading to jQuery 1.5.2 I start 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?

Upvotes: 2

Views: 2030

Answers (3)

codePONPON
codePONPON

Reputation: 1

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/

Upvotes: 0

No Results Found
No Results Found

Reputation: 102745

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

Upvotes: 3

Sangeet Menon
Sangeet Menon

Reputation: 9905

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

Upvotes: 0

Related Questions