2011-02-10 9 views
1

firefox 확장자에서 웹 서버에 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에서이 값을 설정해야 설정이 가능합니다. URL이 존재하는 경우

3

나는 테스트하려면 다음 코드를 사용

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를 보시고 기쁩니다. 새로운 일을하는 방식으로 오래된 주제에 대한 업데이트를 보니 특히 좋았습니다. 아이티카를 계속 지켜주세요! – Noitidart