javascript – Ajax() response not received on time?-ThrowExceptions

Exception or error:

I have tried too many codes to achieve the following:

  $(document).ready(function(){
     var json_data = $.getJSON('url_to_json_encoded_array.php',function(j){
                 return j;
              });
         });
   if(json_data.length > 0){
            //fetch data from the json_data json ecoded array here

       }

But after running the code I always get json_data is with no data, while in developer tab -> network-> i can see that the data loaded in proper mode….

php code which returning the json encoded array is as following:

    <?php 
           echo(json_encode(array("name"=>$name,"email"=>$email)));
     ?>

What is wrong in my code?

How to solve:

Your request is async, you will receive your data inside the callback function

$.getJSON('url_to_json_encoded_array.php', function(data) {
  // here you have it
  console.log(data);
});

Leave a Reply

Your email address will not be published. Required fields are marked *