fetch json value with no key using jquery

105 views Asked by At

i am having trouble fetch JSON using jQuery, if possible someone tell me what mistake i made because my code is not working

data.json file contain

{
"value1",
"value2",
"value3",
"value4"
}

and here is my jquery code

$.getJSON( "data.json", function( data ) {
  $.each( data, function( val ) {
    alert(val);
  });
});

1

There are 1 answers

1
Akash Rajbanshi On BEST ANSWER

use [] instead of {} in the json file as suggested in the comment as your current json is not a valid JSON. Then you can access the file data this way.

$.each(data, function(i,val) {
    alert(val);
  });

Notice, that your current code only access the index of the value but not the actual value. Place the both parameter i and val to get both index and value from the file.

DEMO