I have ajax code which returns JSON data which is just Array of string(no key/val pair).
function loadData() {
$.ajax({
type: 'GET',
url: apiURL,
dataType: "json",
timeout: 2000,
success: onLoadData
};
function onLoadData(data) {
console.log(data);
var arr = JSON.parse(data);
//var arr = jQuery.parseJSON(data); this also fails.
alert(arr[0]);
};
Output of console.log() is = ["one", "two", "three"] but JSON.parse() gives error as :
Uncaught SyntaxError: Unexpected token o
I checked JSON using validator which says this is valid JSON.
Can someone help to understand why parse() is failing?
JSON.parse()an array. If you specifydataType: "json"jquery parses it for you.dataTypetojsonautomatically parses the response asjson