简体   繁体   中英

Call a javascript function after ajax success

I am using the below test code to call a javascript function from ajax method after the success block and pass the success value to a javascript funtion.It's working fine. Now i want to get javascript return message from ajax call. It always return null.

    $.ajax({
      url:"getvalue.php",  
      success:function(data) {
         var retval= change_format(data); 
         alert(retvl);//always return null
         I can't get the result
      }
   });


function change_format(data)
{
 do something.....
 value = "Some Data"; having some data
 return value;
}

Add a callback to your testAjax function

 function testAjax() {
    $.ajax({
      url:"getvalue.php",  
      success:function(data) {
         var retval= change_format(data); 
         alert(retvl);//always return null
         arguments[0](retvl);
        // I can't get the result
      }
    });
}




function change_format(a) {
    // do everything here
}

testAjax(change_format);

The $.ajax sucks. Why not just use the load(). Simple but very powerful. Deploy the algorithm in the code below:

    <div id="stage">RESULT SHOWS HERE</div>
<button id="clickMe">Click Me</button>
<script>
$(document).ready(function() {
$("#clickMe").click(function() {
$("#stage").load("URL?PARAMETERS");
});
});
</script>

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