0

How can I use results of ajax request outside the ajax function?

I send results to other function, it should normally work.

This is the code I used.

function ajaxcall_array(value) { 
    var tmpajaxdata = new Array(); 
    for (var i = 0; i < value.length; ++i){ 
        tmpvar=(value[i]) ;  
        tmpajaxdata.push(tmpvar) ; 
        return tmpajaxdata ; 
        } 
        return tmpajaxdata ; 
        alert(tmpajaxdata); 
        console.debug(tmpajaxdata);
} 

function ajaxcall(){
$.ajax({ 
  url: 'ajax.php',             
  data: "name", 
  type:'GET', 
  async:false, 
  dataType: 'json',                 //data format 
  success: function(results)        //on recieve of reply 
  { 
        ajaxdata = ajaxcall_array(results) ;
        return ajaxdata ;
  } 
}); 
return ajaxdata ;
alert(ajaxdata);
} 
ajaxcall(); 

Thanks

4

1 回答 1

0

这是有效的,但在alert / console.log你已经返回值之前ajaxcall_arrray()

function ajaxcall_array(value) { 
    var tmpajaxdata = new Array(); 
    for (var i = 0; i < value.length; ++i){ 
        tmpvar=(value[i]) ;  
        tmpajaxdata.push(tmpvar) ; 
        return tmpajaxdata ; 
    } 
    return tmpajaxdata ; 

    // HERE ALREADY RETURNED
    // CODE BELOW NEVER HIT
    alert(tmpajaxdata); 
    console.debug(tmpajaxdata);
} 
于 2013-05-05T18:02:01.457 回答