简体   繁体   中英

Getting response value from jQuery AJAX call before success function

I have a system that making it's AJAX calls through jQuery. I want to take all the received data with the request details, without modifying the current uses of $.ajax, and send it to another API for analysis.

I tried to use $.ajaxSetup for that:

$.ajaxSetup({
  beforeSend: function (request, details) {
    request.success(function (response) {
      analysis.sendData(details, response);
    })
  }
})

But the success function from the invocation of $.ajax happened before my success function, and sometimes there are modifications in the response object. Apparently in those cases I'm getting the same, modified, response object.

Is there a way to read the response object before the success function? Thanks

You shouldn't be calling the success function, you should replace it with a function that performs your extra call, then calls the original success function.

$.ajaxSetup({
    beforeSend: function(request, settings) {
        var origSuccess = settings.success;
        settings.success = function(response, textStatus, jqXHR) {
            settings.success = origSuccess;
            analysis.sendData(settings, response, textStatus, jqXHR);
        }
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM