Javascript 通用HTTPClient或Ajax类
下面是我如何进行异步调用的,但是我如何在类中进行异步调用并发送url和Required服务。。。作为参数Javascript 通用HTTPClient或Ajax类,javascript,Javascript,下面是我如何进行异步调用的,但是我如何在类中进行异步调用并发送url和Required服务。。。作为参数 var url = "https://www.appcelerator.com"; var service = someservicename; var xhr = Ti.Network.createHTTPClient({ onload: function(e) { // this.responseText holds the raw text return of
var url = "https://www.appcelerator.com";
var service = someservicename;
var xhr = Ti.Network.createHTTPClient({
onload: function(e) {
// this.responseText holds the raw text return of the message (used for JSON)
// this.responseXML holds any returned XML (used for SOAP web services)
// this.responseData holds any returned binary data
Ti.API.debug(this.responseText);
alert('success');
},
onerror: function(e) {
Ti.API.debug(e.error);
alert('error');
},
timeout:5000
});
xhr.open("post", url);
xhr.send(someservicename);
我希望你期待着这样的事情。你可以用这个,原型就可以了
var JSONCall = function(url,servic, onLoad, onError){
// API Url to call
this.url = url;
this.service = service;
};
JSONCall.prototype = {
call: function(){
var JsonClient = Titanium.Network.createHTTPClient();
JsonClient.open("POST", this.url);
//setting Request Header
JsonClient.setRequestHeader("Content-type", "application/json");
JsonClient.send(service);
JsonClient.onload = this.onLoad;
JsonClient.onerror = this.onError;
}
};
// create callbacks
var onLoad = function(response){ /* do something with response */ },
onError = function(error){ /* do something with error */ };
// create instance
var jsonCall = new JSONCall(url,"servicename", myLoad, myError);
// do a call
jsonCall.call();