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 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();
— asked December 13th 2011 by Olivier Cuny
  • xhr
0 Comments

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.

    — answered December 13th 2011 by Trevor Ward
    permalink
    5 Comments
    • Hi Trevor,

      Thank you for the answer. I did try earlier to have a check for 404 page inside the onLoad function. The issue is that when it goes to the 404 page, I actually get no status returned in the log at all. :-(

      — commented December 13th 2011 by Olivier Cuny
    • no you don't check for a 404 you check for a 200 and error anything else. on xhr.status ….. I don't have example code. mac in office for night, but I will post in the morning if you haven't solved it.

      but check for success full code .

      hope this explains it

      t.

      — commented December 13th 2011 by Trevor Ward
    • Hi Trevor, yes my example code doesn't show my actual attempts to use status in the onload function. The problem is that no matter what I do or attempt, I get absolutely no status returned in the console when it goes to a 404 page. It shows a parsing error in console but never ever get to the status info.

      — commented December 14th 2011 by Olivier Cuny
    • sorry been a busy day….

      ok firstly check if there is a status so..

      if (this.status)
      {
      check here for valid status..
      }
      else
      {
      handle error.
      }

      whilst typing this. noticed that your doing xhr.status when it should be this.status.

      Hope this explains it, if not put a comment here and I will put a full example of code up for you.

      T.

      — commented December 14th 2011 by Trevor Ward
    • No need to apologize at all, you're being kind enough to take the time to answer. Thank you for your patience as well.

      I managed to get to work by using this.status in the onload function. Sorry for the stupid question, but why it would not work with xhr.status? It works for error checking… I am a little puzzled.

      The finsished code looks like this:

      xhr.onload = function() {
          if(this.status == 404) {
              alert("404 status");
              Ti.API.info('Status: ' + this.status);
              return;
          } else {
              var doc = this.responseXML.documentElement;
              quoteTitleLabel.text = doc.getElementsByTagName("title").item(0).text;
              quoteMessageLabel.text = doc.getElementsByTagName("message").item(0).text;
              Ti.API.info('Status: ' + this.status);
          }
      };
      

      — commented December 14th 2011 by Olivier Cuny
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.