开发者

xmlhttprequest in chrome extension

开发者 https://www.devze.com 2023-02-21 04:55 出处:网络
I am developing a chrome extension and I have an iframe which I use in my extension. This is what I do with my iframe

I am developing a chrome extension and I have an iframe which I use in my extension. This is what I do with my iframe

"When I drag and drop a image to my iframe I handle the drop event in one of the content scripts and pass that function call my extension code. There I create a xmlhttprequest object and then send the URL of the image to a php file in my server."

This is what is happening. I get a readyState of "4" but there is n开发者_StackOverflow中文版o POST request going out of my browser. I checked with the "NETWORK" tab in the browser but there is no POST request going out of the browser (I have listed my site in the permissions section of the manifest file).

This is my code --.>

JScript.js(One of the content scripts )

drop: function(event, ui) {
    var imgurl=$(ui.draggable).attr('src');
    imgurl="IMGURL="+imgurl;
    _post("www.somedomain.come/testing.php",imgurl,function(result){ alert("success")});
    }       

This is my proxy in the same content script-->

  _post = function(url, data, callback) 
    {
      console.log("sending post");
      chrome.extension.sendRequest({
      msgType:'post', 
      data: data, 
      url:url
      }, function(response){
        alert(response);
     });
    }

This my OnRequest function handler in background.html -->

 chrome.extension.onRequest.addListener(function(request, sender, sendResponse){
  if (request.msgType === 'post') {
        alert("Now in OnRequest function");
        // console.log("Now in Onrequest Function");
        alert("Url: "+request.url + "\n Data : "+ request.data);
        ajaxcallingfunction(request);
        alert("completed the ajax call");
        sendResponse("success");
      }

    });

var ajaxcallingfunction = function(request){
     var xhr = new XMLHttpRequest();
   xhr.open("POST",request.url, false);
    xhr.onreadystatechange = function(){
    alert(xhr.readyState);
    if (xhr.readyState == 4) {
            alert(xhr.readyState);
        }
    }       
    xhr.send(request.data);
   alert("after xhr call");
};


You have http:// in front of your url, right?

xhr.readyState doesn't tell much, it just means that it is done. Check out what's inside xhr.status, it would contain error code. If everything is ok it should be 200:

xhr.onreadystatechange = function(){
    if (xhr.readyState == 4) {
        alert(xhr.status);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消