Justin Alexander
Justin Alexander

Reputation: 2024

Adding dataTypes to jQuery.ajax?

jQuery.ajax({dataType:...}) supports several known dataTypes (xml, json,jsonp, script,text, or html).

Is there a way to add your own datatype handlers like:

var wcf = function(data){...}

jQuery.ajax({dataType:wcf, ...});

Obviously I've already tried this, and it doesn't work. But is there another way?

Upvotes: 3

Views: 311

Answers (2)

Nick Craver
Nick Craver

Reputation: 630349

There isn't really a clean way to do this, at least not as far as jQuery 1.4.4 simply because there are tons of if() checks inside $.ajax() that rely on datatypes, and that's how they're currently "supported". However, jQuery 1.4.5 will have some interesting changes here.

If you're curious, you can browse github for the latest and see how jQuery AJAX behavior is being made much more extensible by dividing the transport code: https://github.com/jquery/jquery/tree/master/src/transports

Upvotes: 1

Victor Haydin
Victor Haydin

Reputation: 3548

You can create your own implementation of jQuery.ajax function, like:

(function($) {
    var ajax = $.ajax;
    $.ajax = function(o) {
        // perform some custom logic here...
        var result = ajax.apply(this, arguments);
        // ...and here
        return result;
    }
});

Upvotes: 1

Related Questions