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.
13 Answers
-
please post all your http call.
do you return any kind of information from php? In wich format? -
Yes, I return data pulled from a MySQL Database. Purely strings and integers, using JSON.
-
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.
-
Colton
Did you see my previous request for information? -
[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");
-
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?
-
[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.
-
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.
-
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
-
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…
-
Here is my error for using "responseXML" instead of "responseText": Here.
-
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()?
-
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).