1

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?

1 Answers1

1

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);
});
tudor.gergely
  • 4,800
  • 1
  • 16
  • 22