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;
}
6
  • whether change_format uses another ajax function? Commented Sep 19, 2013 at 0:45
  • 1
    It sounds like the problem is with change_format(), not testAjax(). We can't help you if you don't show what it does. Commented Sep 19, 2013 at 0:46
  • I find it nothing wrong.. how will ajax work if testAjax() is not called. Can you put more details? Commented Sep 19, 2013 at 0:57
  • Please see my code again.. Commented Sep 19, 2013 at 0:59
  • 1
    var retval= change_format(data); vs alert(retvl); different variables name retvaland retvl. Commented Mar 17, 2019 at 13:52

2 Answers 2

-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);
Sign up to request clarification or add additional context in comments.

2 Comments

where i add the callback
add the callback function without () as an argument to your calling function
-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>

3 Comments

.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.
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.
.load() is just a shorthand for .ajax with the appropriate options.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.