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

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

问题描述

我是钛的新手,想从我的钛应用调用网络服务.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(endpoints) 有很多 WebServices,那么我将在哪里给出方法名称,即要调用的 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 文档中,函数 openrequest.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 是您的网络服务网址.

address is your webservice url.

method 是您想要调用的方法.

method is the method you desire to invoke.

address+method 是一个 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天全站免登陆