Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

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!

— asked December 5th 2010 by Forrest Frazier
  • createtableview
  • json
  • json.parse
0 Comments

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');
    
    — answered December 5th 2010 by Aaron Saunders
    permalink
    5 Comments
    • I added both setRequestHeader call but I still am getting the same "unable to pars JSON string" error.

      When comparing my json output to the twitter json output I notice that they include the full path to each users avatar. On my feed I am including the file name then in my js I am prepending the url to the file name.

      url:'http://myurl.com/thumbnails/'+drinkThumb,
      

      Could this be a problem or do you think the feed is not being read at all by my app?

      — commented December 5th 2010 by Forrest Frazier
    • if you log the output of responseTxt, can you post it so we can see what exactly is coming back from the server?

      — commented December 5th 2010 by Aaron Saunders
    • For whatever reason my build.log file is empty. However I set up a test json output from my server available at http://www.seemydrink.com/drinks/test_stream if that helps.

      — commented December 5th 2010 by Forrest Frazier
    • my first observation is that the response headers from your server say Content-Type:text/html
      ;while the response headers from the twitter request say Content-Type:application/json; charset=utf-8

      — commented December 5th 2010 by Aaron Saunders
    • Great find. I have changed the output so the response header is now correct.

      — commented December 5th 2010 by Forrest Frazier
  • instead of this

    var drinks = eval('('+this.responseText+')');
    

    did you try

    var drinks = JSON.parse(loader.responseText);
    
    — answered December 5th 2010 by Aaron Saunders
    permalink
    2 Comments
    • When I try that I get the following error

      [WARN] Exception in event callback. {
      line = 16;
      message = "Unable to parse JSON string";
      name = SyntaxError;
      sourceId = 247344848;
      sourceURL = "file://localhost/_sites/Appcelerator/MyDrinkApp/Resources/main_window/stream.js";
      }
      

      Line 16 is the line that was changed.

      — commented December 5th 2010 by Forrest Frazier
    • something strange… when I put the sample in a text file, it loads and parses fine

      — commented December 5th 2010 by Aaron Saunders
  • 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)));
    
    — answered December 5th 2010 by Aaron Saunders
    permalink
    1 Comment
    • When I changed my response headers it removed this comment and is now working great. Thanks for all your help.

      — commented December 5th 2010 by Forrest Frazier
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.