How to Call a WebService in titanium using javascript

给你一囗甜甜゛ 提交于 2019-12-03 16:41:16

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';
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!