2011-02-10 10 views
1

firefox拡張機能からWebサーバーにPOSTリクエストを送信したいと思います。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(); 

が、私はどのようにXMLHttpRequestオブジェクトを使用する方法について

答えて

4

「をWrappedNativeのプロパティを変更カント」に関するエラーが出ます。 拡張開発で同じ起点ポリシーがありません

+0

私のコードは、私ははComponents.utils.importを使用してインポートしていますモジュールであり、アクセスすることができていないようですし、新しいXMLHttpRequestオブジェクトを作成します。 –

+2

@ user329931 'Components.classes [" @ mozilla.org/xmlextras/xmlhttprequest;1 "]。createInstance();' – Neil

1

requestMethodはnsIHttpChannelの属性ですので、設定する前にgChannelでQueryInterfaceを呼び出す必要があります。

3

私は、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); 
} 
+1

本当に別の仲間のfiefox addonを見てうれしいstackoverflow寄稿者。新しい方法で作業を進めていくために、古いトピックのアップデートを見るのがいいですね。それをintikaにしてください! – Noitidart

関連する問題