function getXhr() {
if (typeof Ti !== 'undefined') {
return Ti.Network.createHTTPClient();
}
if (typeof XMLHttpRequest) {
return new XMLHttpRequest();
}
throw Error('Unknown environment');
}
var xhr = getXhr();
xhr.onload = function () {console.log('onload' + ' ' + xhr.status)};
xhr.onerror = function () {console.log('onerror' + ' ' + xhr.status)};
xhr.addEventListener('load', function() {console.log('load' + ' ' + xhr.status)});
xhr.addEventListener('error', function() {console.log('error' + ' ' + xhr.status)});
xhr.open('GET', 'http://www.httpbin.org/status/400'); //returns 400 status
xhr.send();
Console output in browser:
{noformat}
onload 400
load 400
{noformat}
Output in Titanium:
{noformat}onerror{noformat}
Hey [~s.volkov], can you add a bit more context here? 400-500 error codes usually return in the
onerror
callback. Theload
anderror
*events* should not even exist. But I am open for improvements here, although it would mean a quite huge breaking change for advanced use-cases.I'm not insisting on events implementation (nevertheless, it would be nice to have fullspec compilant xhr). Problem is that XMLHttpRequest does not call onerror callback on status >=400, but Titanium does. You can try example above in any browser (of course in "www.httpbin.org" origin).