从Firefox扩展发送POST请求
问题描述:
我想从Firefox扩展发送POST请求到Web服务器。从Firefox扩展发送POST请求
我发现这个例子发送POST请求; https://developer.mozilla.org/en/Creating_Sandboxed_HTTP_Connections#HTTP_notifications
但我不能得到它的工作。
我目前有这样的代码;
var ioService = Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService);
var uri = ioService.newURI("http://www.google.com", null, null);
gChannel = ioService.newChannelFromURI(uri);
postData = "a=1&b=2&c=3";
var inputStream = Components.classes["@mozilla.org/io/string-input-stream;1"]
.createInstance(Components.interfaces.nsIStringInputStream);
inputStream.setData(postData, postData.length);
var uploadChannel = gChannel.QueryInterface(Components.interfaces.nsIUploadChannel);
uploadChannel.setUploadStream(inputStream, "application/x-www-form-urlencoded", -1);
uploadChannel.requestMethod = "POST";
uploadChannel.open();
,但我得到一个错误“不能修改WrappedNative的性质”
答
关于使用XMLHttpRequest对象如何。 扩展开发中没有相同的来源策略
答
我使用下面的代码来测试一个URL存在:
if(typeof XMLHttpRequest == "undefined"){
var req = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"]
.createInstance();
}
else{
var req = new XMLHttpRequest();
}
try{
req.open("GET", "https://" + request.URI.host + request.URI.path);
var timeOutID = httpNowhere._getWindow().setTimeout(function() {
req.abort();
Services.prompt.alert(null,"Info","nok "+ "https://" + request.URI.host
+ request.URI.path);
}, (redirectTimer));
req.onreadystatechange = function (e) {
if (req.readyState==4){
Services.prompt.alert(null,"Info","ok "+ "https://" + request.URI.host
+ request.URI.path);
req.abort();
}
}
req.send(null);
} catch (err) {
Services.prompt.alert(null,"Info",err);
}
我的代码,我使用Components.utils.import导入一个模块中,它似乎并没有能够访问和创建新的XMLHttpRequest对象。 – 2011-02-10 09:22:48
@ user329931`Components.classes [“@ mozilla.org/xmlextras/xmlhttprequest; 1”]。createInstance();` – Neil 2011-02-10 20:20:25