Use ajax data in multiple functions

multiple ajax calls promise
problem with multiple ajax requests
multiple ajax calls simultaneously jquery
multiple ajax request on same page
multiple ajax calls in loop
how to stop multiple ajax requests
ajax success: function
ajax done function

I have an ajax call that looks like this. My question is, with the callback function I can get the data out from myAjaxCall() to another function so I can work on the data. But is it possible to do multiple callbacks, so I can pass the data to two, three or four different functions that do all different things to the data?

Or do I have to do multiple ajax request for every function that I want to use?

function myAjaxCall() {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', 'myJSON.json', true);

    xhr.onload = function() {
        if(this.status === 200) {
            var data = JSON.parse(this.responseText);
            callback && callback(data);
        }
    };
    xhr.send();
}

You can just wrap the callbacks in another function and use that wrapper function instead:

function callback(data) {
    callbackOne(data);
    callbackTwo(data);
    callbackThree(data);
    . . . 
}

If you need broader use of data than this, you'd need to resolve a Promise or something similar to get the data into a broader scope.

How to call multiple method in one ajax function, Hi, $(document).ready(function () { $.ajax({ type: 'POST', url: 'MasterLanding.aspx/​GetCustomerCount', data: "{}", contentType: 'application/json;  Here's what works for me after 2 days of head-scratching; why I couldn't get the AJaX 'data' setting to send two key/values (including a variable containing raw image data) was a mystery, but that seems to be what the jQuery.param() function was written for; create a params array with your variables, without quotes:

why not just call different functions with data as a parameter, sth. like this:

function myAjaxCall() {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'myJSON.json', true);

xhr.onload = function() {
    if(this.status === 200) {
        var data = JSON.parse(this.responseText);
        functionA(data);
        functionB(data);
        functionC(data);
    }
};
xhr.send();
}

and inside your function do with data whatever you want to do:

functionA(data) {
   // TODO do sth. with data
}

Multiple Simultaneous Ajax Requests (with one callback) in jQuery , Multiple Simultaneous Ajax Requests (with one callback) in jQuery object (​probably Ajax request), // Deferred object (probably Ajax request) ).then(function(​)  Specifies the "this" value for all AJAX related callback functions: data: Specifies data to be sent to the server: dataFilter(data,type) A function used to handle the raw response data of the XMLHttpRequest: dataType: The data type expected of the server response. error(xhr,status,error) A function to run if the request fails. global

Another way could be this:

function myAjaxCall(callbacks) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', 'myJSON.json', true);

    xhr.onload = function() {
        if(this.status === 200) {
            var data = JSON.parse(this.responseText);
            callbacks && callbacks.forEach(function(callback) {
                callback(data);
            })
        }
    };
    xhr.send();
}

// Where callback1 and callback2 are both functions
myAjaxCall([callback1, callback2]);

Handling Sequential AJAX Calls using jQuery, There is a requirement to make multiple AJAX calls parallelly to fetch the used to hit first which was resulting in fetching the inconsistent data to the jQuery.​when() provides a way to execute callback functions based on  Are you trying to save this value? If you are using AJAX then there is no way for you to hook into a function since the page is already loaded. If you want to achieve that, you will have to not use AJAX, save your value to the DB and then use that saved value into your GF field.

AJAX & Deferreds, jQuery provides the $.ajax method — and several convenience methods — to When you're working with a JSON response to an XHR, jQuery takes care of this​  How to call multiple method in one ajax function [Answered] RSS 5 replies Last post Aug 06, 2015 11:57 AM by bruce (sqlwork.com)

Multiple AJAX Callback functions, When you use add_action() with that hook name, it lets you run a function in response to AJAX requests with the associated action name. So to handle AJAX​  In our simple use case, we can use jQuery's $.when () method, which takes a list of these "Deferred" objects (All jQuery Ajax methods return Deferred objects) and then provides a single callback. $.when ( // Deferred object (probably Ajax request), // Deferred object (probably Ajax request), // Deferred object (probably Ajax request

jQuery.when(), Description: Provides a way to execute callback functions based on zero or more by jQuery.ajax() is a Promise-compatible object and can be used this way: In the case where multiple Deferred objects are passed to jQuery.when() , the Each argument is an array with the following structure: [ data, statusText, jqXHR ]. The jQuery $.ajax() function is used to perform an asynchronous HTTP request. It was added to the library a long time ago, existing since version 1.0. The $.ajax() function is what every function discussed in the previously mentioned article calls behind the scene using a preset configuration.

Comments
  • Have you tried passing it to your other functions? Where did you get stuck?