如何使用 javascript 在钛中调用 WebService
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8709033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Call a WebService in titanium using javascript
提问by Ajeet Pratap Maurya
I am new to titanium and and want to call a web service from my titanium app.
The webService returns the json response.
As I am aware of calling the webService using XMLRPC
but very confused regarding json.
我是钛的新手,想从我的钛应用调用网络服务。webService 返回 json 响应。因为我知道使用调用 webServiceXMLRPC
但对 json 非常困惑。
Until now, I know that we have to create the HTTPClient
.
到现在为止,我知道我们必须创建HTTPClient
.
var request = Titanium.Network.createHTTPClient();
request.open("POST", "http://test.com/services/json");
request.onload = function() {
var content = JSON.parse(this.responseText);//in the content i have the response data
};
request.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); //did not understand this line
request.send();
Now the problem is if my url(endpoints) have many WebServices, so where i will give the method name i.e WS name which is to be called.
现在的问题是,如果我的 url(endpoints) 有很多 WebServices,那么我将在哪里给出方法名称,即要调用的 WS 名称。
From the API documentation of Titanium mobile the function open
i.e. request.open
accepts 3 parameters:
从 Titanium mobile 的 API 文档中,该函数open
ierequest.open
接受 3 个参数:
method name (http method name)
url of request
async (boolean property) by default true.
方法名(http 方法名)
请求地址
异步(布尔属性)默认为真。
In the above code what is "POST"
doing there?? and if my WS name is system.connect
then where i will be mentioning that in code?
上面的代码在"POST"
做什么?如果我的 WS 名称是system.connect
我将在代码中提及的地方?
And what if the WS needs parameter, so how can we send the parameter to the webService form the above code.
而如果WS需要参数,那么我们如何将参数从上面的代码中发送到webService。
I know that request.send()
can be used to send parameter but how ??
我知道request.send()
可以用来发送参数但是怎么做??
回答by Canastro
To invoke a webservice you should:
要调用网络服务,您应该:
// create request
var xhr = Titanium.Network.createHTTPClient();
//set timeout
xhr.setTimeout(10000);
//Here you set the webservice address and method
xhr.open('POST', address + method);
//set enconding
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
//send request with parameters
xhr.send(JSON.stringify(args));
// function to deal with errors
xhr.onerror = function() {
};
// function to deal with response
xhr.onload = function() {
var obj = JSON.parse(this.responseText);
};
addressis your webservice url.
地址是您的网络服务网址。
methodis the method you desire to invoke.
method是您想要调用的方法。
address+method is a URL, in your example: "http://test.com/services/json" the method invoked would be named json.
address+method 是一个 URL,在您的示例中:“http://test.com/services/json”调用的方法将被命名为 json。
args: is a json object where it's variable names should have the exact same name as the webservice parameters. You can create a the parameters object like this:
args: 是一个 json 对象,它的变量名应该与 webservice 参数具有完全相同的名称。您可以像这样创建一个参数对象:
var args = {};
args.parameter1 = 'blabla';
args.parameter2 = 'blaaaa';