最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to Call a WebService in titanium using javascript - Stack Overflow

programmeradmin2浏览0评论

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", "");
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:

  1. method name (http method name)

  2. url of request

  3. 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 ??

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./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:

  1. method name (http method name)

  2. url of request

  3. 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 ??

Share Improve this question edited Jan 3, 2012 at 8:47 puk 16.8k31 gold badges124 silver badges205 bronze badges asked Jan 3, 2012 at 7:12 Ajeet Pratap MauryaAjeet Pratap Maurya 4,2543 gold badges30 silver badges46 bronze badges 1
  • Could you give more information about the services you are trying to call? REST or SOAP endpoints? – tomconte Commented Jan 6, 2012 at 13:19
Add a ment  | 

1 Answer 1

Reset to default 14

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./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';
发布评论

评论列表(0)

  1. 暂无评论