我在for循环中发送多个Titanium.Network.createHTTPClient()。
当我处理返回文本时,我无法理解哪个响应对应于每个send()。
如何区分多个createHTTPClient()返回对象??
url[0] = http://api.yahoo.com?key=0
url[1] = http://api.yahoo.com?key=1
url[2] = http://api.yahoo.com?key=2
var xhrArray = new Array();
for (var k = 0 ; k < 3;k++){
var v = encodeURIComponent(url[k]);
xhrArray[k] = Titanium.Network.createHTTPClient();
xhrArray[k].autoEncodeUrl = false;
xhrArray[k].onload = function(e){
var response = JSON.parse(xhrArray[k].responseText);
//It doesnt work, How can I distinguish multiple xhr return??
};
xhrArray[k].open('GET', url[k], true);
xhrArray[k].send();
}
答案 0 :(得分:1)
将自定义属性分配给HTTPClient实例
xhrArray[k].myProperty = 'Hello';
从this
例如:this.myProperty
完整示例:
var url = [
'http://api.yahoo.com?key=0',
'http://api.yahoo.com?key=1',
'http://api.yahoo.com?key=2'
],
xhrArray = [];
for (var k = 0; k < 3; k++) {
//var v = encodeURIComponent(url[k]);
xhrArray[k] = Titanium.Network.createHTTPClient();
xhrArray[k].autoEncodeUrl = false;
// Assign your custom property
xhrArray[k].myProperty = k;
xhrArray[k].onerror = function(e) {
var response = JSON.parse(xhrArray[this.myProperty].responseText);
};
xhrArray[k].open('GET', url[k], true);
xhrArray[k].send();
}