Reputation: 65
i need to make two different ajax calls and pass both the json data to another function. how can i do it using below ajax call format?
$.ajax({
url:'',
type:'GET',
dataType: 'json',
success: callme
});
$.ajax({
url:'',
type:'GET',
dataType: 'json',
success: callme
});
function callme(JSON1,JSON2){
}
Upvotes: 3
Views: 150
Reputation: 119
You can make the ajax as synchronize call by setting the async property as false. so that after callme function would be called after succeding both the requests.
var JSON1, JSON2
$.ajax({
url: url,
type:'GET',
dataType: 'json',
async: false,
success: function(data){JSON1=data}
});
$.ajax({
url: url,
type:'GET',
dataType: 'json',
async: false
success: function(data){JSON2=data}
});
callme(JSON1,JSON2);
function callme(JSON1,JSON2){
}
Thats it.
Upvotes: -1
Reputation: 71928
This is the perfect use case for $.when
(assuming you're using jQuery):
var ajax1 = $.ajax({
url:'',
type:'GET',
dataType: 'json'
});
var ajax2 = $.ajax({
url:'',
type:'GET',
dataType: 'json'
});
$.when(ajax1, ajax2).done(function(json1, json2) {
// both requests succeeded
}).fail(function(){
// one or both requests failed
});
The done
callback will only be called when both requests have completed successfully; the fail
callback will be called if any request fails.
Upvotes: 6
Reputation: 370
My idea here would be to create a getData function you can pass a url to it and have a callback. In the last callback do callme()
passing both returned data objects.
function callme(JSON1, JSON2) {}
function getData(url, fn) {
$.ajax({
url: url,
type: "GET",
dataType: "json",
success: fn
});
}
getData("url", function(JSON1) {
getData("url", function(JSON2) {
callme(JSON1, JSON2);
});
});
Upvotes: 0
Reputation: 76405
You can't expect two separate ajax calls to be processed simultaneously, doubly so because JS is single threaded (and AJAX calls are asynchronous, lest you specify they're not - but that is to be avoided). Also: You can't expect JS to take into account the number of arguments you specified for any function and know that it shouldn't invoke the function until both arguments have a value.
Lastly: could you specify what your question actually is: you're asking about how to call another function, but in both cases you seem to be passing the same success callback. Could you elaborate on what data both calls are expected to return, and what data you're sending, too?
If you want a function to be called only after both calls were successful, you could use a closure:
var callme = (function()
{
var JSON1, JSON2;
return function(response)//this function will receive each response separately
{
JSON1 = JSON1 || response;//if JSON1 isn't set, assign current response
if (JSON1 !== response)
{//JSON1 was already set, second response is in
JSON2 = response;
//code to process both responses at once goes here
//but make them undefined before returning, if not, a second call won't work as expected
JSON1 = undefined;
JSON2 = undefined;
}
};
}();
$.ajax({url: 'some/url',
data: yourData1,
type: 'GET',
success: callme});
$.ajax({url: 'second/url',
data: yourData2,
type: 'GET',
success: callme});
Do bear in mind that it's crucial that the closure (callme
function) precedes the AJAX call: because of the way callme
is assigned a function (as an expression), the function declaration is not hoisted!
Upvotes: 0
Reputation: 12018
You'll need to chain your functions together so that the first AJAX result calls the second function and the second AJAX result calls the callme function:
$.ajax({
url:',
type:'GET',
dataType: 'json',
success: function(data) {
$.ajax({
url:',
type:'GET',
dataType: 'json',
success: function(data2) {
callme(data, data2);
}
});
}
});
Upvotes: 0
Reputation: 5100
Nest your ajax calls
$.ajax({
url:',
type:'GET',
dataType: 'json',
success: function(JSON1) {
$.ajax({
url:',
type:'GET',
dataType: 'json',
success: function(JSON2) {
callme(JSON1,JSON2);
}
});
}
});
function callme(JSON1,JSON2){
}
Upvotes: 1
Reputation: 32286
$.ajax({
url:',
type:'GET',
dataType: 'json',
success: callme1
});
$.ajax({
url:',
type:'GET',
dataType: 'json',
success: callme2
});
var JSON1;
var JSON2;
function callme1(JSON){
JSON1 = JSON;
if (JSON2)
callme();
}
function callme2(JSON){
JSON2 = JSON;
if (JSON1)
callme();
}
function callme() {
// Do whatever you need with JSON1 and JSON2.
}
Upvotes: 2