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();
您需要根据需要如何与其他全局对象一起工作来调整它。但希望这能让您朝着正确的方向前进。
关于javascript - 通用JSON调用类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6289634/