0

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;
}
user2176150
  • 303
  • 3
  • 12
  • 21

2 Answers2

-1

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);
Ryan
  • 14,392
  • 8
  • 62
  • 102
-3

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>
DUG
  • 319
  • 1
  • 3
  • 7
  • `.load()` and `.ajax()` have completely different purposes. Your example assumes that the response should be rendered on the page exactly as its received, whereas OP's example shows that he's trying to use the result in a separate function. And why does `$.ajax` *"suck"*? Consider providing some sort of basis for your claim, otherwise it means nothing. – Tyler Roper Jul 15 '19 at 20:02
  • Not to mention you're answering a question from 2013. Now I don't think that's inherently bad, sometimes new information presents itself and it's good to keep older questions up-to-date. However, for the reasons I mentioned above, I don't believe that's the case here. – Tyler Roper Jul 15 '19 at 20:05
  • `.load()` is just a shorthand for `.ajax` with the appropriate options. – JJJ Jul 15 '19 at 21:48