如何使用javascript调用钛中的WebService [英] How to Call a WebService in titanium using javascript

查看:96
本文介绍了如何使用javascript调用钛中的WebService的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

到目前为止,我知道我们必须创建 HTTPClient

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

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

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.

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

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


  1. 方法名称(http方法名称) )

  1. method name (http method name)

请求的网址

async(布尔属性)默认为true。

async (boolean property) by default true.

在上面的代码中,什么是POST在那里做??如果我的WS名称是 system.connect 那么我将在代码中提到它?

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?

如果WS需要参数,那么我们如何将参数发送到webService表格上面的代码。

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

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

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

推荐答案

要调用网络服务,你应该:

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.

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

address+method is a URL, in your example: "http://test.com/services/json" the method invoked would be named json.

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

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';

这篇关于如何使用javascript调用钛中的WebService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆