如何使用JavaScript在钛中调用WebService(How to Call a WebService in titanium using javascript)

编程入门 行业动态 更新时间:2024-10-25 19:29:37
如何使用JavaScript在钛中调用WebService(How to Call a WebService in titanium using javascript)

我是钛新手,并想从我的钛应用程序调用Web服务。 webService返回json响应。 因为我知道使用XMLRPC调用webService,但对json非常困惑。

到现在为止,我知道我们必须创建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();

现在的问题是,如果我的网址(端点)有很多Web服务,那么我将在哪里给出方法名称,即要调用的WS名称。

从Titanium mobile的API文档中,函数open即request.open接受3个参数:

方法名称(http方法名称)

请求的网址

异步(布尔属性)默认为true。

在上面的代码中, "POST"在那里做什么? 如果我的WS名是system.connect那么我会在代码中提到这一点?

而如果WS需要参数,那么我们怎样才能将参数发送到webService形式的上述代码。

我知道request.send()可以用来发送参数,但是如何?

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.

Until now, I know that we have to create the 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.

From the API documentation of Titanium mobile the function open i.e. request.open accepts 3 parameters:

method name (http method name)

url of request

async (boolean property) by default true.

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?

And what if the WS needs parameter, so how can we send the parameter to the webService form the above code.

I know that request.send() can be used to send parameter but how ??

最满意答案

要调用web服务,您应该:

// 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); };

地址是您的webservice网址。

方法是你想要调用的方法。

地址+方法是一个URL,在你的例子中:“http://test.com/services/json”调用的方法将被命名为json。

args :是一个json对象,它的变量名应该与webservice参数具有完全相同的名称。 你可以像这样创建一个参数对象:

var args = {}; args.parameter1 = 'blabla'; args.parameter2 = 'blaaaa';

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); };

address is your webservice url.

method is the method you desire to invoke.

address+method is a URL, in your example: "http://test.com/services/json" the method invoked would be named 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:

var args = {}; args.parameter1 = 'blabla'; args.parameter2 = 'blaaaa';

更多推荐

本文发布于:2023-07-30 01:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1321384.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   JavaScript   WebService   javascript   titanium

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!