Titanium Community Questions & Answer Archive

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

XHR pulling from MySQL DB Problems

So I am using the latest build of 1.5.0.

Whenever I open a page that has an HTTP client that pulls remote MySQL database information via PHP, I get this in the console:

Entity: line 1: parser error : Start tag expected, '<' not found
{"catalog":[{"id":"1","name":"Scooter","description":"A nice scooter
^
[ERROR] Error Domain=com.google.GDataXML Code=-1 "The operation couldn’t be completed. (com.google.GDataXML error -1.)". in -[TiDOMDocumentProxy parseString:] (TiDOMDocumentProxy.m:50)

It loads fine, I'm just a fan of having a clean console when I test/release my Apps. I don't like bugs/errors/warnings. Of any kind.

— asked November 16th 2010 by Colton Arabsky
  • 1.5.0
1 Comment
  • what does you onload call look like

    — commented November 16th 2010 by Aaron Saunders

13 Answers

  • please post all your http call.
    do you return any kind of information from php? In wich format?

    — answered November 16th 2010 by Stefano Di Luca
    permalink
    0 Comments
  • Yes, I return data pulled from a MySQL Database. Purely strings and integers, using JSON.

    — answered November 17th 2010 by Colton Arabsky
    permalink
    0 Comments
  • Colton, would you please isolate which specific Titanium command produces this error? You can achieve this by inserting Ti.API.info() statements into your code. Keep doing so, until you found the precise offending line. This will help to understand how to circumvent it.

    — answered November 18th 2010 by Paul Dowsett
    permalink
    0 Comments
  • Colton
    Did you see my previous request for information?

    — answered November 18th 2010 by Paul Dowsett
    permalink
    0 Comments
  • [INFO] Before XHR
    Entity: line 1: parser error : Start tag expected, '<' not found
    {"seasoneps":[{"id":"1","episode":"1","season":"1","title":"That's Disgusting","
    ^
    [ERROR] Error Domain=com.google.GDataXML Code=-1 "The operation couldn’t be completed. (com.google.GDataXML error -1.)". in -[TiDOMDocumentProxy parseString:] (TiDOMDocumentProxy.m:50)
    [INFO] After XHR
    

    Here is my code:

            Ti.API.info("Before XHR");
            var xhr = Titanium.Network.createHTTPClient();
            xhr.onload = function(){
            var json = JSON.parse(this.responseText);
            var jsoncats = json.seasoneps;
            Ti.API.info("After XHR");
    
    — answered November 19th 2010 by Colton Arabsky
    permalink
    0 Comments
  • Colton

    You haven't provided the information I requested - precisely which one of the commands between the before and after messages you have inserted is actually outputting the error?

    — answered November 19th 2010 by Paul Dowsett
    permalink
    0 Comments
  • [INFO] Before XHR
    [INFO] Before XHR OnLoad
    Entity: line 1: parser error : Start tag expected, '<' not found
    {"seasoneps":[{"id":"18","episode":"1","season":"3","title":"What Else Can You D
    ^
    [ERROR] Error Domain=com.google.GDataXML Code=-1 "The operation couldn’t be completed. (com.google.GDataXML error -1.)". in -[TiDOMDocumentProxy parseString:] (TiDOMDocumentProxy.m:50)
    [INFO] After XHR OnLoad
    [INFO] Before JSON Parse
    [INFO] After Jsoncats define
    
            Ti.API.info("Before XHR");
            var xhr = Titanium.Network.createHTTPClient();
            Ti.API.info("Before XHR OnLoad");
            xhr.onload = function(){
            Ti.API.info("After XHR OnLoad");
            var json = JSON.parse(this.responseText);
            Ti.API.info("Before JSON Parse");
            var jsoncats = json.seasoneps;
            Ti.API.info("After Jsoncats define");
    

    So it's the xhr.onload = function() line.

    — answered November 19th 2010 by Colton Arabsky
    permalink
    1 Comment
    • That's great, Colton - thank you.

      — commented November 19th 2010 by Paul Dowsett
  • Colton

    I think the problem is that you don't need to parse this.responseText at all, because it isn't a JSON string. It is already a JS object, and so you can access your data directly. See the following example:

    var data =
        {
            "cityLocation": {
                "cityLocationName": "Dallas-Fort Worth, TX",
                "code": "DFW",
                "countryCode": "US",
                "countryName": "United States",
                "latitude": 32.9,
                "longitude": -97.0366666666667,
                "name": "Dallas-Fort Worth",
                "rank": 8,
                "state": "TX",
                "stateName": "Texas"
            }
        };
            // this following line is commented
            // because it would give you an exception
            // var myobj = JSON.parse(data);
            Ti.API.info("State is: "+ data.cityLocation.stateName);
    

    Ti.API.info() would return "Texas".

    Therefore, in your code, var json = JSON.parse(this.responseText) is not necessary, because your data is available in this.responseText.seasoneps.id. Thus, this.responseText.seasoneps.id would return 18.

    — answered November 19th 2010 by Paul Dowsett
    permalink
    0 Comments
  • Colton

    Have you fixed this? An alternative solution to the one in my last answer is to use the responseXML rather than the responseText property. Hence, try the following code:

    var xhr = Titanium.Network.createHTTPClient();
    xhr.onload = function(){
    var json = JSON.parse(this.responseXML);
    var jsoncats = json.seasoneps;
    

    I'd be grateful to know whether any of my solutions worked or not :-/

    Thanks

    — answered November 19th 2010 by Paul Dowsett
    permalink
    0 Comments
  • Hey Hal, it doesn't work. It just gives me even more errors.

    I can't get the exact errors to you right now, but tomorrow morning I will post with them. Would it be possible I could get your Email address so we could discuss this quicker than on the Appcelerator forums? These aren't the best form of question-answering around…

    — answered November 20th 2010 by Colton Arabsky
    permalink
    1 Comment
    • Hi Colton

      Have you tried both of my solutions, from each of the last two answers I posted?

      I can empathise that you need to resolve this quickly, but this Q&A really is the best place for it, as others can benefit from the solution if they run into the same issue in future.

      — commented November 20th 2010 by Paul Dowsett
  • Here is my error for using "responseXML" instead of "responseText": Here.

    — answered November 20th 2010 by Colton Arabsky
    permalink
    3 Comments
    • OK, that's not the solution then. What about my previous suggestion, to access the data directly rather than parsing it with JSON.parse()?

      — commented November 20th 2010 by Paul Dowsett
    • Hal, it is pulled from a PHP script that pulls it from a MySQL Database. You can view the PHP file here. Wouldn't that make it necessary to parse it with JSON?

      — commented November 20th 2010 by Colton Arabsky
    • Pastie PHP

      — commented November 20th 2010 by Colton Arabsky
  • Hal, it is pulled from a PHP script that pulls it from a MySQL Database. You can view the PHP file here. Wouldn't that make it necessary to parse it with JSON.parse()?

    — answered November 20th 2010 by Colton Arabsky
    permalink
    2 Comments
    • Would you mind just trying it, for my peace of mind?

      — commented November 20th 2010 by Paul Dowsett
    • I don't even know if that is possible, since the PHP script encodes it as JSON.

      — commented November 20th 2010 by Colton Arabsky
  • Colton

    Try this:

    var xhr = Titanium.Network.createHTTPClient();
    xhr.onload = function(){
    var thisTitle = this.responseText.seasoneps.title;
    Ti.API.info('This shows title is: '+thisTitle);
    

    Obviously you will need to complete the function (it is missing the closing bracket).

    — answered November 21st 2010 by Paul Dowsett
    permalink
    0 Comments
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.