最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - $.ajax combining with $.ajaxSetup default settings - Stack Overflow

programmeradmin2浏览0评论

I have a way of keeping track of current Ajax Requests which involves $.ajaxSetup(), and counting beforeSends & pletes.

jsFiddle Demo

var ajaxProcs = 0;

$.ajaxSetup({
    beforeSend : function () { ajaxProcs++; },
    plete   : function () { ajaxProcs--; }
});

The problem is, if any given $.ajax() call has beforeSend or plete it will throw off the correct count of Ajax requests running.

For example, this would overwrite it, and not add 1 to the ajaxProcs count.

$.ajax({
    beforeSend : function () { /* blah blah */ }
});

Is it possible to have BOTH ajaxSetup AND the $.ajax Override run?

I have a way of keeping track of current Ajax Requests which involves $.ajaxSetup(), and counting beforeSends & pletes.

jsFiddle Demo

var ajaxProcs = 0;

$.ajaxSetup({
    beforeSend : function () { ajaxProcs++; },
    plete   : function () { ajaxProcs--; }
});

The problem is, if any given $.ajax() call has beforeSend or plete it will throw off the correct count of Ajax requests running.

For example, this would overwrite it, and not add 1 to the ajaxProcs count.

$.ajax({
    beforeSend : function () { /* blah blah */ }
});

Is it possible to have BOTH ajaxSetup AND the $.ajax Override run?

Share Improve this question edited Jun 26, 2015 at 16:59 Sumurai8 20.8k11 gold badges68 silver badges102 bronze badges asked Jul 30, 2013 at 19:02 Mark Pieszak - Trilon.ioMark Pieszak - Trilon.io 67.2k15 gold badges83 silver badges96 bronze badges 1
  • You can overwrite the jQuery ajax function and add your own independent code to do the counting. Very ugly and unreliable but possible. – Lee Meador Commented Jul 30, 2013 at 19:05
Add a ment  | 

4 Answers 4

Reset to default 4

How about this, let's try beforeSend() function as a sample..

$.ajaxSetup({
    beforeSend: function(){
        console.log('execute another beforeSend() function'); 
    }
});

Then this is your Ajax function:

$.ajax({
    beforeSend : function () { 
        console.log('execute beforeSend() function');
        if('beforeSend' in $.ajaxSettings){
            $.ajaxSettings.beforeSend();
        }
    }
...

And now it will execute the both functions

Hope it helps

Try this:

var ajaxProcs = 0;
$(document).ajaxSend(function(){
  ajaxProcs++;
});
$(document).ajaxComplete(function(){
  ajaxProcs--;
});

This will run on every jQuery ajax call, regardless of what callbacks you bind to the individual requests.

ajaxSetup() provides default values for parameters that can be passed to the ajax() function. They only apply as defaults and not when supplied in the actual ajax() call.

JQuery's ajaxStart(), ajaxSend() and ajaxComplete() are things you might want to read about. They are called before and after each ajax call.

There is a global option on the individual ajax() call that will disable those global functions though.

Not really a plete answer because it is overridable but maybe helpful.

Because of how jQuery is designed I think your best bet is to make a rule to call the ajaxSetup function manually from the beforeSend. Even if jQuery had a more OO approach you would still have to call the parent anyway - it won't magically call itself.

http://jsfiddle/mrcJ7/

var ajaxProcs = 0;

var setupObj = {
    beforeSend : function(xhr) { ajaxProcs++; console.log('Increased to: ' + ajaxProcs); },
    plete   : function() { ajaxProcs--; console.log('Decreased to: ' + ajaxProcs); }
};

$.ajaxSetup(setupObj);

$.ajax({
    url: 'http://fiddle.jshell',
    beforeSend : function (xhr) { 
        setupObj.beforeSend(xhr);
    }
});
发布评论

评论列表(0)

  1. 暂无评论