简体   繁体   English

Ajax成功后调用JavaScript函数

[英]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. 我使用下面的测试代码在成功块之后从ajax方法调用javascript函数并将成功值传递给javascript函数。它工作正常。 Now i want to get javascript return message from ajax call. 现在我想从ajax调用中获取javascript返回消息。 It always return null. 它总是返回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 向您的testAjax函数添加回调

 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. $ .ajax很烂。 Why not just use the load(). 为什么不只使用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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM