简体   繁体   English

如何使用javascript调用钛中的WebService

[英]How to Call a WebService in titanium using javascript

I am new to titanium and and want to call a web service from my titanium app. 我是钛的新手,想从我的钛应用程序中调用一个Web服务。 The webService returns the json response. webService返回json响应。 As I am aware of calling the webService using XMLRPC but very confused regarding json. 因为我知道使用XMLRPC调用webService但是对于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 ie WS name which is to be called. 现在问题是如果我的url(端点)有许多WebServices,那么我将给出方法名称,即要调用的WS名称。

From the API documentation of Titanium mobile the function open ie request.open accepts 3 parameters: 从Titanium mobile的API文档开始,函数openrequest.open接受3个参数:

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

  2. url of request 请求的网址

  3. async (boolean property) by default true. async(boolean property)默认为true。

In the above code what is "POST" doing there?? 在上面的代码中什么是"POST"在那里做? and if my WS name is system.connect then where i will be mentioning that in code? 如果我的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()可以用来发送参数但是如何?

To invoke a webservice you should: 要调用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);

    };

address is your webservice url. 地址是您的webservice网址。

method is 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. args :是一个json对象,其变量名称应与webservice参数具有完全相同的名称。 You can create a the parameters object like this: 您可以像这样创建一个参数对象:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM