Pasando datos a .ajaxSent y .ajaxComplete

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?

preguntado el 31 de enero de 12 a las 16:01

1 Respuestas

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 ¿Cómo puedo obtener valores de cadena de consulta en JavaScript? will help (I know, I've done it).

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

contestado el 23 de mayo de 17 a las 15:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.