0
success: function(data) {
    console.log(data);
    console.log(data[0].name);
}

Output on console:

[{"id":1,"name":"Apple"}]
undefined

Where is problem in the code? I want to use this output in tag

<option value="' + data[0].id + '"'>' + data[0].name + </option>
Narendra Jadhav
  • 10,052
  • 15
  • 33
  • 44
Rajnish
  • 13
  • 9
  • 2
    "_... after parsing ..._" but you're not parsing `data` ... Notice also, that JS variables are not parsed into HTML markup like it seems you're expecting them to be parsed. – Teemu May 11 '18 at 10:37
  • You can refer on this https://stackoverflow.com/questions/45015/safely-turning-a-json-string-into-an-object – Marck Antonio May 11 '18 at 10:40

1 Answers1

4

Your data variable seems to be string. You will need to parse it first.

Try following

data = JSON.parse(data);
console.log(data[0].name);

For reference, JSON.parse

Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59
  • Have you tried the same way as the answer? Did the log correctly showed the data or is it somewhere else in the code? – Nikhil Aggarwal May 11 '18 at 10:46
  • Yes the same way as in the answer I am getting data from server as: [{"id":1,"name":"Apple"}] its in the form of an array – Rajnish May 11 '18 at 10:54