[英]Firefox addon sdk Request module POST method with payload
I need to do a POST method inside a firefox add-on to another server, I have been trying to use different ways, and after googling I found out that I should use the Request module from the SDK inside my main.js. 我需要在另一个服务器的firefox插件中执行POST方法,我一直在尝试使用不同的方式,在进行谷歌搜索之后,我发现我应该在main.js中使用SDK中的Request模块。
I am using firefox v 23 我正在使用firefox v 23
I tried using the chrome module 我尝试使用chrome模块
var xmlhttp = chrome.Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
.createInstance(chrome.Ci.nsIXMLHttpRequest);
but I got NS_ERROR_FAILURE . 但是我得到了NS_ERROR_FAILURE 。 I even added the permissions in the package.json
我什至在package.json中添加了权限
"permissions": {
"cross-domain-content": ["https:[some url]"]
}
But it still gives the same error. 但是它仍然给出相同的错误。
I then used the Request module but didn't work so far. 然后,我使用了“请求”模块,但到目前为止还无法正常工作。 I tried a GET method with the Request module and it works fine.
我尝试了带有请求模块的GET方法,并且效果很好。 But the POST method always returns a 0 status and an empty response.
但是POST方法始终返回0状态和空响应。
I tried doing the same request via a browser http client and it worked fine!! 我尝试通过浏览器http客户端执行相同的请求,但效果很好! But through the code inside the add-on it always returns a 0.
但是通过插件中的代码,它始终返回0。
The request sets headers and of course has a payload. 该请求设置标题,并且当然具有有效负载。
var contentObject = {[Valid JSON Object]};
var myRequest = Request({
url: "https://[some url]",
headers: {
"pragma": "no-cache"
},
content: contentObject,
contentType: "application/json",
onComplete: function (response) {
console.log("Status: " + response.status);
console.log("Response json: " + JSON.stringify(response));
}
}).post();
Your support is highly appreciated. 非常感谢您的支持。 There are very few resources I found over the internet about this issue and non of them solved my problem.
我在互联网上发现的有关此问题的资源很少,没有一个解决了我的问题。
I guess the server script expects a JSON string representation of the contentObject. 我猜服务器脚本期望contentObject的JSON字符串表示形式。 But this is not how objects are treated by the request module, they are turned to key/value pairs.
但这不是请求模块对待对象的方式,而是将它们转换为键/值对。
So change 所以改变
content: contentObject
to 至
content: JSON.stringify(contentObject)
the POST method always returns a 0 status and an empty response
POST方法始终返回0状态和空响应
This might not be direct answer, but I had the same problem last couple of days. 这可能不是直接的答案,但是最近几天我遇到了同样的问题。 A friend who was connected to network via different provider tried the same code and it worked fine.
一个通过不同的提供商连接到网络的朋友尝试了相同的代码,并且运行良好。 Also, if I remember correctly, I could connect to the port 80 but not to the port where I was sending POST request so that port might be blocked on the network you are connected.
另外,如果我没记错的话,我可以连接到端口80,但不能连接到我发送POST请求的端口,因此该端口可能在您连接的网络上被阻止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.