使用json调用webService在Titanium中会出现错误,但使用curl从终端调用webService效果良好

Calling webService using json gives error in Titanium but calling webService from terminal using curl works fine

本文关键字:调用 webService curl 终端 错误 Titanium json 使用      更新时间:2023-09-26

嗨,我正在尝试使用json调用titanium中的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}

我认为url是错误的,但当我试图从我的终端调用相同的web服务时,即使用curl实用程序

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

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

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