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

Javascript 使用json调用webService会出现错误,但使用curl从终端调用webService效果良好,javascript,json,titanium,appcelerator,Javascript,Json,Titanium,Appcelerator,嗨,我正在尝试使用json调用Tianium中的Web服务。 该Web服务不接受任何参数,所以我只能调用它 这是我的密码: var xhr = Titanium.Network.createHTTPClient(); xhr.setTimeout(10000); xhr.open("POST","http://mytesturl.net/services/json/system.connect"); xhr.setRequestHeader("Content-Type", "applica

嗨,我正在尝试使用json调用Tianium中的Web服务。 该Web服务不接受任何参数,所以我只能调用它

这是我的密码:

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的一部分传递。您需要在函数调用中添加
method=system.connect
作为
data
参数,并将URL更改为与curl请求中相同的URL(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”)这是我的工作。