2011-03-30 117 views
1

我正在開發一個鉻擴展,我有一個iframe,我在我的擴展中使用。這是我用我的iframe做的事xmlhttprequest在擴展中的鉻

「當我將圖像拖放到我的iframe中時,我在其中一個內容腳本中處理放置事件,並通過該函數調用我的擴展代碼,然後創建一個xmlhttprequest對象並然後將圖像的URL發送到我的服務器中的php文件。「

這就是發生了什麼事。我得到一個「4」的readyState,但沒有POST請求從我的瀏覽器中去除。我使用瀏覽器中的「NETWORK」選項卡進行了檢查,但瀏覽器沒有POST請求(我在清單文件的權限部分列出了我的網站)。

這是我的代碼 - >

JScript.js(其中一個內容腳本)

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

這是我在同一個內容腳本代理 - >

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

這是我OnRequest函數處理程序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"); 
}; 

回答

0

你有http://在網址前,對不對?

xhr.readyState不會說太多,它只是意味着它完成了。看看xhr.status裏面有什麼,它會包含錯誤代碼。如果一切正常,應該是200

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

謝謝!這工作,但現在的問題是我能夠發送請求,但不是數據。每當我嘗試獲取圖像的URL時,它都會顯示爲空字符串。我的php腳本使用簡單的$ _POST [「IMGURL」]數組來獲取圖像的URL。任何想法,爲什麼我總是得到一個空字符串?我確信我在將數據發送到服務器之前已經獲得了數據(圖像的url)。我錯過了一些基本的東西? – Ajai 2011-03-31 04:25:34

+0

@Ajai在Chrome控制檯中打開網絡選項卡,並檢查該請求中發送的數據。如果一切看起來都不錯,那麼在php端執行'print_r($ _ POST)'並檢查收到的內容。 – serg 2011-03-31 04:35:25