Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery.ajax converter not called

I'm having trouble with jQuery.ajax converters - I can't get my converter to be called.

I've got this jQuery AJAX code (simplified for the question) :

    $.ajax({
    url: "http://myurl/myservice",
    dataType: "JSONP",
    cache: false,
    success: function (data, textStatus, jqXHR) { /* do stuff */ },
    error: function (jqXHR, textStatus, errorThrown) { /* do stuff */ },
    timeout: 5000,
    converters: { "JSONP": myConversionFunction }
});

When I use this code, the converter function myConversionFunction isn't being called. I want to use the converter to convert dates in the response as show in other SO questions but just can't get it firing.

Using fiddler I've checked the response and it is JSONP, with content type "application/x-javascript".

Any ideas what I'm doing wrong?

Thanks, Chris.

like image 774
Chris Avatar asked Feb 22 '23 10:02

Chris


1 Answers

I think you can't overwrite jQuery's default converters like json. Introduce your own converter instead (and include text in your specifier, as in this case it's a conversion from text to your output):

$.ajax({
    url: "http://myurl/myservice",
    dataType: "jsonp myConversion",
    cache: false,
    success: function (data, textStatus, jqXHR) { /* do stuff */ },
    error: function (jqXHR, textStatus, errorThrown) { /* do stuff */ },
    timeout: 5000,
    converters: {
        "text myConversion": function(value) {
            console.log("pre-processing...");
            /* do stuff */
            return value;
        }
    }
});

like image 144
Julian D. Avatar answered Mar 03 '23 17:03

Julian D.