0

I'd like to handle some custom data in global ajaxComplete and ajaxSent event. In my case, I want info about the zone that will be updated. ex. of what I want to achive.

$.get(
    'http://someUrl',
    { param : value, param2: value2 },
    'extraData', //<-- here, for exemple
    function (data){
    }
);

$(document).ajaxSend(function(e, xhr, options){
   var data = //Some way to get back 'extraData';
   doSomethingWhenSendingRequest(data);
});

$(document).ajaxComplete(function(e, xhr, options){
   var data = //Some way to get back 'extraData';
   doSomethingWhenRequestCompleted(data);
});

I need the same with $.post, $.getJSON... Is there a built-in way to achieve this?

Johnny5
  • 6,664
  • 3
  • 45
  • 78

1 Answers1

2

You can get the data from the options, or settings as :

 $(document).ajaxComplete(function (event, xhr, settings) {
   console.log(settings.data); // do stuff with querystring style KVP's
 });

... So, in this output you'd see your params. If you need a way to get at them easily, a slight modification to Artem's code at How can I get query string values in JavaScript? will help (I know, I've done it).

Hope this helps someone, know it's late in.

Community
  • 1
  • 1
coderage
  • 21
  • 2