XHR Request, can't check for error for 404 page or other errors
For the life of me, I cannot figure out how to implement an error function if the status of a page is 404 or if the XML file has any issue. If I set the remote file to a non existent file on a valid server address, I do not get an error, but of course it cannot parse XML, because the page is a 404 page.
Any idea, on how to check for a 404 page using example code below
var xhr = Titanium.Network.createHTTPClient();
// Error Check
xhr.onerror = function(e)/* on error in getting data from server */
{
//check response status and act accordingly.
if((xhr.status == 0) || (xhr.status != 200)) {
Ti.API.info('Status Error: ' + xhr.status);
alert("The service is currently unavailable. Please Try Again Later.");
return;
}
};
xhr.onload = function() {
//Ti.API.info('quotes xml ' + this.responseXML + ' text ' + this.responseText);
var doc = this.responseXML.documentElement;
quoteLabelTitle.text = doc.getElementsByTagName("title").item(0).text;
quoteLabel.text = doc.getElementsByTagName("message").item(0).text;
Ti.API.info('Status: ' + xhr.status);
};
// open the client
xhr.open('GET', 'http://test.com/appdev/quotes.xml');
// send the data
xhr.send();
1 Answer
-
Accepted Answer
on your onload function add your status check. if a valid say 200 status then process or error. the onerror function is for failure but a 404 as you say is valid you just have to handle it cleanly.
the way you try to handle it in the onerror functions your solution to the onload function.
Hope this helps.
T.