【问题标题】:Calling webService using json gives error in Titanium but calling webService from terminal using curl works fine使用 json 调用 webService 在 Titanium 中会出错,但使用 curl 从终端调用 webService 可以正常工作
【发布时间】:2012-02-25 23:53:25
【问题描述】:

您好,我正在尝试使用 json 在钛中调用 webService。 该 webService 不接受任何参数,所以我只需要调用它。

这是我的代码:

var xhr = Titanium.Network.createHTTPClient();
xhr.setTimeout(10000);

xhr.open("POST","http://mytesturl.net/services/json/system.connect");  
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
xhr.send();
xhr.onerror = function() {

Titanium.API.info("some thing is wrong in calling");
};

xhr.onload = function() {

Titanium.API.info("The API response is " + this.responseText);
};

在日志上我得到这个错误:

The API response is {"#error":true,"#data":"Invalid method ","#response_code":405}

我认为网址有误,但是当我尝试从终端调用相同的 Web 服务时,即使用 curl 实用程序

curl --data method=system.connect http://mytesturl.net/services/json

我得到了我需要的回应.. 我在这里做错了什么??

【问题讨论】:

    标签: javascript json titanium appcelerator


    【解决方案1】:

    您没有将任何有效负载传递给服务器,而是尝试将方法作为 URL 的一部分传递。您需要在 send 函数调用中添加 method=system.connect 作为 data 参数,并将 URL 更改为与 curl 请求 (http://mytesturl.net/services/json) 中的相同。

    【讨论】:

    • 你能帮我一些代码吗..现在我尝试了var data = {"method":"system.connect"}然后xhr.send(JSON.stringify(data));我是正确的
    • 看服务是否需要JSON格式的数据。基于 curl 示例,我猜它需要application/x-www-form-urlencoded 格式的数据。所以你应该尝试传递xhr.send("method=system.connect");
    • 非常感谢......希望我能不止一次投票给你......:)
    • 如果没有 xhr.send("method=system.connect"); 我这个工作。
    猜你喜欢
    • 2013-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多