0

I want to get the json object via a ajax call. On using alert inside the call the alert show the expected result but when using the alert outside the block give 'undefined'. i want to use the same json object outside the block. if i using var result , its also give the same result which i get from event_array. I am not getting what is the problem

var event_array;
var results;
get_app_cal();
function get_app_cal() {
  jQuery.ajax({
    url:'<?php echo site_url();?>/xyzController/xyz_cal',
    type:'POST',
    dataType: "JSON",
    success:function(results) {
      //alert(results);
      //event_array = JSON.stringify(results)
      event_array = results;
      console.log(event_array);
      alert(event_array) // its show the required value (work perfectly)
    }
  });
}
alert(event_array) //  return undefined (Not working not showing required value)
kashif
  • 193
  • 2
  • 19
  • Check for reference - http://stackoverflow.com/questions/7104474/how-does-asynchronous-javascript-execution-happen-and-when-not-to-use-return-st – Nikhil Aggarwal Mar 01 '17 at 11:18
  • @nikhil its not working for me – kashif Mar 01 '17 at 11:25
  • kashif - Well the main idea about sharing the link was to provide you some information about the behavior of ansync functions. You can refer to the another answer as referenced by @satpal. If still not clear then I will provide an answer for the same. – Nikhil Aggarwal Mar 01 '17 at 11:29
  • @nikhil thank you for quick reply . i am new to javascript therefore its not clear how syncronous and asyncronous work can you tell me the how can . – kashif Mar 01 '17 at 11:34
  • As this question is marked duplicate, I can not answer over here. Can you please share me your email address. I will revert there – Nikhil Aggarwal Mar 01 '17 at 11:41

0 Answers0