success:function (data) { console.log(data); gives ["name", "price"] ok
console.log(data[0]); gives nothing
Probably. the only time this can happen is that data is not an object / array, but a regular string. Apparently the server does not set the content-type headers in applcation/json , and jquery does not know about the data type (based on these headers, a conversion occurs). So, either do it forcibly
success: function(data){ data = JSON.parse(data); console.log(data[0]); } either use $.getJSON instead of $.get or $.ajax with type: "GET" . For POST requests, obviously, only the first option, or configure server headers
PS: judging by your previous question, on the server side you should do so
$data = array('name' => $name, 'price' => $price); $data = json_encode($data); header("Content-Type: application/json"); print ($data); die() after print - teranSource: https://ru.stackoverflow.com/questions/770536/
All Articles