Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-2383] HTTPClient doesn't trigger "onerror" with 4xx and 5xx HTTP response codes

GitHub Issuen/a
TypeBug
PriorityMedium
StatusClosed
ResolutionFixed
Resolution Date2011-10-27T15:53:51.000+0000
Affected Version/sRelease 1.7.0
Fix Version/sSprint 2011-32, Release 1.8.0
ComponentsiOS
Labelsn/a
ReporterVance Lucas
AssigneeBlain Hamon
Created2011-04-15T03:18:19.000+0000
Updated2011-10-27T15:53:51.000+0000

Description

When working with the HTTPClient, I was expecting a 404 response to trigger my "onerror" function, but it never did. Even a 500 "Internal Server Error" response did not trigger the "onerror" function. Everything I tried just executed "onload", regardless of the actual server's response.

More useful behavior would be similar to jQuery's ajax method:
http://api.jquery.com/jQuery.ajax/">http://api.jquery.com/jQuery.ajax/

  • 1xx and 2xx responses trigger the "success" callback
  • 4xx and 5xx responses trigger the "error" callback

Comments

  1. Anirudh Nagesh 2011-07-05

    This is reproducible on android too! Associated helpdesk ticket: http://support-admin.appcelerator.com/display/9600 Increasing the customer priority to 'within release'. Ex: The 'xhr_error.js' in kitchensink demonstrates the same.
  2. Kincy Clark 2011-07-11

    Assigning back to Ani for sample code creation.
  3. Anirudh Nagesh 2011-07-11

    Sample code is same as xhr_error.js in kitchensink. Have attached the same.
       var win=Titanium.UI.currentWindow;
       
       var l1 = Titanium.UI.createLabel({
       	text:'UTF-8 GET',
       	font:{fontSize:16,fontWeight:'bold'},
       	top:10,
       	width:300,
       	left:10,
       	height:'auto'
       });
       win.add(l1);
       
       var l2 = Titanium.UI.createLabel({
       	text:'Waiting for response...',
       	font:{fontSize:13},
       	top:40,
       	left:10,
       	width:300,
       	height:'auto',
       	color:'#888'
       });
       win.add(l2);
       
       var xhr = Titanium.Network.createHTTPClient();
       
       xhr.onload = function(e)
       {
       	l2.text = e.responseText;
       	Ti.API.info('im inside onlaod');
       };
       
       xhr.onerror = function(e)
       {
       	l2.text = e.error;
       	Ti.API.info('im inside onerror');
       };
       
       // open the client
       xhr.open('GET','http://www.fre100.com');
       
       // send the data
       xhr.send();
       
       
  4. Anirudh Nagesh 2011-07-25

    I shall update this ticket with the server side code by EOD!
  5. Dawson Toth 2011-07-27

    I added an action to appc.me for testing this out. Change the "code=" to some other code (like 403, 500, etc) and the server will respond appropriately. If you look at the response sent from the server, you should also see a custom status description is getting returned as well.
       var win = Ti.UI.createWindow({ backgroundColor: '#fff' });
       var lbl = Ti.UI.createLabel({
           text: 'Waiting for response...', color: '#000',
           top: 40, left: 10,
           width: 300
       });
       win.add(lbl);
       win.open();
       
       var xhr = Ti.Network.createHTTPClient({
           onload: function(e) {
               alert('FAIL! "onload" ran when we got the error code ' + this.status + ' from the server!');
           },
           onerror: function(e) {
               alert('Error: ' + e.error);
           }
       });
       xhr.open('GET', 'http://appc.me/test/simulateresponse?code=404');
       xhr.send();
       
  6. Dawson Toth 2011-07-27

    WORKS properly on Android! Does not work on iPhone Simulator 4.3.
  7. Anirudh Nagesh 2011-07-27

    Oh Dawson has done it already:) Thanks Dawson. Yes, it works fine on Android.
  8. Eric Merriman 2011-09-15

    Onerror is now triggered when an error is returned, but the e.error is returning undefined. Closing this bug as fixed and opened another TIMOB-5294 describing the new problem. Verified fixed with Titanium Studio, build: 1.0.5.201109091616, SDK 1.8.0.v20110914184549 on an iPad 2, 4.3.5 and iPod touch 3rd Gen 4.0.2.

JSON Source