0

I need to use in some function the value of variable, which is generated inside internal function of ajax success. Contrary to the rules, the new value is not available outside of ajax function. However if I add alert after the internal function - variable becomes available! Here is clear example of what I mean. In this case alert will show nothing and variable is not available for the subsequent javascript:

$(document).ready(function() {
function somename() {
var exRate = 0;

$.ajax({
      type: 'POST',
      url: 'checkrate.php',
      data: 'pic_url=' + picurl,
      success: function(data){
        exRate = data;
     } // end ajax success function
 }); // end ajax
alert(exRate);
}
});

BUT in this case (code is the same, just two alerts) - second alert already shows correct value of the new variable:

$(document).ready(function() {
function somename() {
var exRate = 0;

$.ajax({
      type: 'POST',
      url: 'checkrate.php',
      data: 'pic_url=' + picurl,
      success: function(data){
        exRate = data;
     } // end ajax success function
 }); // end ajax

alert(exRate); 
alert(exRate); 
}
});

Please help!

Polundra
  • 1
  • 1
  • 3
    `AJAX` => asynchronous. `alert` => blocking. Describe your real problem, taking into account that 'below `$.ajax` call' doesn't mean 'after `$.ajax` call is resolved'. – raina77ow Jun 17 '14 at 12:27
  • 1
    You will have to perform what you need INSIDE the success response... – Joakim M Jun 17 '14 at 12:28
  • Thank you guys, I see the reason. After reading of above-stated topic and couple of tests I found that it doesn't work for my code anyway - so in this case was simpler to remove ajax request at all.:( – Polundra Jun 17 '14 at 17:54

0 Answers0