Now I am trying to return array of strings with the response to a $.get() request , so I converted the string array to JSON object
var messagesFromFile = Array();
messagesFromFile[0]="code";
messagesFromFile[1]="eat";
messagesFromFile[2]="play";
messagesFromFile[3]="sleep";
response.writeHead(200,{'Content-Type':'application/json'})
response.write(JSON.stringify(messagesFromFile));
response.end();
, then in the front end ,I wanted to convert the json object back to string array
$(window).load(function(){
$.get("http://localhost:8000",
{
name:"GetAllMessages"
},function(data,status){
try{
var arr = $.parseJSON(data);
}
catch(err){
alert(err);
}
});
});
but an "syntax error unexpected token" exception is thrown !!! any help to overcome this exception or any way to return array of strings with the response thanks in advance
Generally jQuery will already have parsed the JSON for you, especially if the content type header is set correctly. My guess is that you're trying to decode an already decoded object. Try putting console.log("GET results: type: %s data: %s", typeof data, data) before the $.parseJSON call.