开发者

Adding dataTypes to jQuery.ajax?

开发者 https://www.devze.com 2023-02-02 18:58 出处:网络
jQuery.ajax({dataType:...}) supports several known dataTypes (xml, json,jsonp开发者_开发知识库, script,text, or html).

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?


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


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

精彩评论

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

关注公众号