var JsonClientPatientSearch = Titanium.Network.createHTTPClient();
// API Url to call
var url = GetAPIUrl() + "PatientSearch";
JsonClientPatientSearch.open("POST", url);
//setting Request Header
JsonClientPatientSearch.setRequestHeader("Content-type", "application/json");
JsonClientPatientSearch.send(PatientSearch(PatientSearchCriteria,Credentials,Header));
JsonClientPatientSearch.onload = function(){
};
JsonClientPatientSearch.onerror = function(e){
};
我的项目中有很多JSON调用,是否可以编写一个类并使用其实例进行JSON调用...只传递参数...
最佳答案
您可以创建对象实例并重用它们。您的代码如下所示:
var MyCall = function(url, onLoad, onError){
// API Url to call
this.url = GetAPIUrl() + url;
this.onLoad = onLoad;
this.onError = onError;
};
MyCall.prototype = {
call: function(){
var JsonClientPatientSearch = Titanium.Network.createHTTPClient();
JsonClientPatientSearch.open("POST", this.url);
//setting Request Header
JsonClientPatientSearch.setRequestHeader("Content-type", "application/json");
JsonClientPatientSearch.send(PatientSearch(PatientSearchCriteria,Credentials,Header));
JsonClientPatientSearch.onload = this.onLoad;
JsonClientPatientSearch.onerror = this.onError;
}
};
// create callbacks
var myLoad = function(response){ /* do something with response */ },
myError = function(error){ /* do something with error */ };
// create instance
new MyCall("PatientSearch", myLoad, myError);
// do a call
MyCall.call();
您需要根据与其他全局对象一起使用的方式进行调整。但是希望这能给您正确的方向。