//******************Example for multiple ajax success and failure*************//
function doAjax1(){
var data="ajax_mode=request1&sample_data1=sample_data1";
return $.get("ajax.php",data,function(msg){
//alert(msg);
});
}
function doAjax2(){
var data="ajax_mode=request2";
return $.get("ajax.php",data,function(msg){
//alert(msg);
});
}
//success and error cant be used with $.when()
$.when( doAjax1(), doAjax2() ).then(function(msg){
alert('alert oncompletion of both ajax'+msg); //Not returns both ajax result
console.log( 'I fire once BOTH ajax requests have completed!' );
}).fail(function(){
console.log( 'I fire if one or more requests failed.' );
}).done(function(){
console.log( 'I fire if one or more requests done.' );
});
//****************************************************************************//
PHP Codes ajax.php
<?php
if( isset($_REQUEST['ajax_mode']) )
{
$ajax_mode=trim($_REQUEST['ajax_mode']);
switch($ajax_mode){
case 'request1':
$sample_data1=$_REQUEST['sample_data1'];
$checking="checking ajax req 1";
echo $sample_data1;
break;
case 'request2':
$checking="checking ajax req 2";
echo $checking;
break;
}
}
?>
Question: function doAjax1, doAjax2 returns the value from server.
I am going to use $.when() for multiple ajax request
i need to get all ajax return values once all ajax request had done.
i had used $.when().then(function(msg){alert(msg)})
Result of alert sample_data1,success,[object Object][Please Explain me why it alerts like this]
My Expected alert result sample_data1, sample_data2
1 Answer