-1
var json = $.getJSON(url);
console.log(json.item);

It prints undefined in console, why?

What type of data will return when I use $.getJSON?

And how can I load the JSON file as a Object?

I have search many key words but I can't get a solution.

greuze
  • 4,250
  • 5
  • 43
  • 62

1 Answers1

0

That function is actually making an ajax call and won't set var json, in your code. So, you need a function to process the callback from the server.

$.getJSON( "test.js", function( json ) {
   console.log( "JSON Data: " + json.users[ 3 ].name );
});

I pulled that example from here.

Snake14
  • 416
  • 2
  • 7