루프는 XHR 객체의 onload
속성에 바인딩되는 함수에 있어야 다음 HttpClient를가 ansynchronous 작동
function jsonPOST(inAction, inParams) { // jsonPOST is a global function
var xhr = Ti.Network.createHTTPClient({
onload : function(e) {
Ti.API.info("Received text: " + this.responseText);
for (i in this.responseText) {
tblListing.appendRow({ title: myTest[i].title, id: myTest[i].id });
}
return this.responseText;
},
onerror : function(e) {
Ti.API.debug(e.error);
alert('error');
return false;
},
timeout : 8000, // in milliseconds
});
var sendData = {
'action' : inAction,
'json' : JSON.stringify(inParams)
};
xhr.open('POST', "http://domain.com/file.php"); // url redacted
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr.send(sendData);
} // end. jsonPOST()
var myTest = jsonPOST('getlisting'); // only need to pass first param in this instance
참고. 요청을 보내고 데이터를 기다리지 만 호출 스크립트는 계속 실행됩니다.