Parse error bringing json data into Table View
I am trying to show a list of recent website activity in a TableView within my app. I have based this code off of the Twitter example. I know my json is valid but for some reason when I try to test this page I get the following error.
[WARN] Exception in event callback. {
line = 1;
message = "Parse error";
name = SyntaxError;
sourceId = 257342536;
}
My code and and an example of my json data is available at http://pastie.org/1349653
Any help would be much appreciated.
Thanks!
3 Answers
-
Accepted Answer
maybe setting content-type? it is difficult for me to test since I can't access the server
loader.setRequestHeader('Content-Type', 'application/json; charset=utf-8'); loader.setRequestHeader('Accept', 'application/json');
-
instead of this
var drinks = eval('('+this.responseText+')');
did you try
var drinks = JSON.parse(loader.responseText);
-
the problem is the comment you have on the end of the JSON response…
if you remove it, your code will work… sometimes, it is the little things.
// this is 16 chars long... <!-- 0.1426s --> // remove it from the output and it parses var len = xhr.responseData.text.length - 16 Ti.API.log(JSON.parse(xhr.responseData.text.substring(0,len)));