Issue
When setting the async parameter as false in the HttpClient the callbacks unload and on error are not being called in Ti SDK3.3.0 and 3.4.0, the same code in SDK 3.2.3.GA the callback are correctly executed.
Test code
Test case attached (network.js)
app.js
var win = Ti.UI.createWindow({
backgroundColor:'white'
});
var xhr = require('commonjs/xhr');
var url = "http://roi-stg.mcd.com/mobile/localization/au/v3.1/iPhone/image_resources.zip";
var sync = true;
function doCall(_url,_method,_sync){
xhr.doCALL(_method,_url,{},callback,_sync);
}
function callback(_args) {
alert("HTTPClient " + _args);
Ti.API.info("HTTPClient " + _args);
}
var view = Ti.UI.createView({
top:75,
layout:'vertical'
});
var bt_HEAD = Ti.UI.createButton({
title: "XHR HEAD"
});
var view2 = Ti.UI.createView({
layout:'horizontal'
});
var label = Ti.UI.createLabel({
text:'SYNC',
width:Ti.UI.SIZE
});
var basicSwitch = Ti.UI.createSwitch({
value:true // mandatory property for iOS
});
view2.add(label);
view2.add(basicSwitch);
basicSwitch.addEventListener('change',function(e){
Ti.API.info('Switch value: ' + basicSwitch.value);
sync = basicSwitch.value;
});
bt_HEAD.addEventListener('click',function(e){
doCall(url,'HEAD',sync);
});
view.add(bt_HEAD);
view.add(view2);
win.add(view);
win.open();
xhr.js
exports.doCALL = function (_method,_url, _params, _callback,_sync){
// XHR POST
var xhr = Titanium.Network.createHTTPClient();
xhr.onload = function(e)
{
_callback(this.responseText);
};
xhr.onerror = function(e)
{
_callback('XHR >>> '+JSON.stringify(e));
};
xhr.open(_method, _url, _sync);
xhr.send();
};
Steps to repro
1. Run test case
2. Use the Sync switch and set to false
3. Press XHR HEAD
Expected Result:
An alert showing the result of the httpClient call.
Actual Result:
No alert is shown as the callbacks are not called.
Is this not a duplicate of TIMOB-17432?
If you're making a SYNC call, there are no callbacks, the code will wait for the "send" call to be done. Try this:
Documentation: http://docs.appcelerator.com/titanium/latest/#!/api/Titanium.Network.HTTPClient-method-send
Marking this as invalid since making asynchronous calls on the HTTP Client does not fire the callback, this is by design. The sample in my previous comments should solve the issue.
This is a behavior change. In ASIHttp, we specifically hacked around the behavior to make a callback on a synchronous call. In NSURLConnection, we decided this was no longer a good idea and stopped it. In general, we strongly recommend you not use synchronous network calls unless there is a good rationale of doing so.
Closing ticket as invalid.