简体   繁体   中英

Ajax success return value use in other function

On file upload, I want ajax hit the url like:

$.ajax(
    { url: '<?php echo base_url();?>apk-reader/ApkInfo.php',
      data: {"apps": "../apps/apk_files/"+file, user_id: "<?php echo $this->session->userdata('user_id'); ?>"},
      type: 'get',
      success: function(output) { alert(output);}

Here I want to store the out put in another variable and use it in OnComplete of another ajax load.

Call other function from success and pass the result to it, SomeFunction(output);

$.ajax({
      url: '<?php echo base_url();?>apk-reader/ApkInfo.php',
      data: {"apps": "../apps/apk_files/"+file, user_id: "<?php echo $this->session->userdata('user_id'); ?>"},
       type: 'get',
      success: function(output) { 
           alert(output);SomeFunction(output)
       }
  });

Declare a variable in global scope and store your output in that varibale. Now you can access the global variable at the callback of any other ajax method,

var result1;
$.ajax( { url: 'apk-reader/ApkInfo.php', data: {"apps": "../apps/apk_files/"+file, user_id: "session->userdata('user_id'); ?>"}, type: 'get', success: function(output) { alert(output);
result1=output;
}
$.ajax( { url: 'apk-reader2/ApkInfo.php', data: {"apps": "../apps/apk_files/"+file, user_id: "session->userdata('user_id'); ?>"}, type: 'get', success: function(output2) { alert(output2);
alert(result1);

}

you can use this code :

$.ajax({
      url: 'your URL',
      data: {"your Parameter"},
       type: 'get',
      success: function(data) { 
           AjaxMethode(data)
       }
  });

function AjaxMethode(data){
    if(data.d){
        //someCode
    }
}

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