我正在使用appcelerator studio构建服务Android。
在此服务中,调用返回json的Web服务。
所以这是调用ws的代码:
funtcion start(){
var obj = getDocument("CFDECTEST02",NULL);
Titanium.API.info("DOCUMENT "+ obj);
}
function getDocument(fiscalCode, date){
//se il servizio può partire, procedo con il chiamare il ws
// create request
var obj;
var xhr = Titanium.Network.createHTTPClient();
//set timeout
xhr.setTimeout(10000);
//Here you set the webservice address and method
xhr.open('POST', "http://Document/");
//set enconding
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
var myObject = {
cf :fiscalCode,
date_last_synchronization :date
};
//send request with parameters
xhr.send(JSON.stringify(myObject));
// function to deal with errors
xhr.onerror = function() {
Ti.API.info("SERVIZIO IN ERRORE");
Ti.API.info(this.responseText);
disattivaSemaforo();
};
// function to deal with response
xhr.onload = function() {
var obj = JSON.parse(this.responseText);
Ti.API.info(this.responseText);
return obj;
};
}
现在我希望如果WS返回正确的响应,我会将响应返回给方法start。
然后,当我尝试调用Web服务时,一切正常,方法
xhr.onload = function() {
是执行,我可以将obj返回给start()方法。
这样做可能吗?