1

在我的Chrome擴展程序中,我能夠訪問我的'punch.php'文件並接收數據,問題是我似乎無法將數據傳遞給文件。這裏是我的代碼:Ajax未發佈數據,Chrome擴展程序

jQuery.fn.punch = function(){ 
    $(this).click(function(){ 
     var punchBtn = $(this); 
     var ProjectMemberId = '1' 
     var ProjectId = '1' 
     var str = 'ProjectMemberId='+ProjectMemberId+'&ProjectId='+ProjectId; 

     var xhr = new XMLHttpRequest(); 
     xhr.open("POST", "http://www.ontimepunchcard.com/scripts/punch.php", true); 
     xhr.onreadystatechange = function() { 
      if (xhr.readyState == 4) { 
       $('#PunchBox').html(xhr.responseText); 
       if(xhr.responseText==0) 
       { 
        punchBtn.removeClass('PunchedIn'); 
       } 
       else 
       { 
        punchBtn.addClass('PunchedIn'); 
       } 
      } 
     } 
    xhr.send('ProjectMemberId='+ProjectMemberId+'&ProjectId='+ProjectId); 
    }); 
}//END PUNCH METHOD 

這是一個正常的web應用程序與所有相同的代碼的重複。 Id的帖子爲'punch.php',並且是SQL語句的一部分。該聲明失敗了Chrome擴展,並返回一個錯誤語句,所以我開始迴應實際的SQL查詢,看看它試圖做什麼。結果是帶有兩個Id的SQL查詢丟失,因此錯誤和查詢失敗。

我在這裏有語法問題嗎?有沒有可能,儘管我有權限設置谷歌說我應該的方式,我的擴展只能接收數據而不發送?這裏還有其他的愚蠢嗎?

+0

`str`永遠不會被使用。那是故意的嗎?你在哪裏試圖在代碼示例中傳遞數據? – bzlm 2011-01-09 22:20:58

回答

2

「?你確定你有適當的權限,在您的擴展後的數據到那個文件我只是做了一個簡單的代碼片段,我回到‘1’的responseText 。據我所見,只要它在擴展頁面中運行(不是內容腳本),片段就是好的 - Mohamed Mansour 1月9日23:45「

顯然這是答案。

1

嘗試增加Content-typeContent-length標題:

var str = 'ProjectMemberId='+ProjectMemberId+'&ProjectId='+ProjectId; 

var xhr = new XMLHttpRequest(); 
xhr.open("POST", "http://www.ontimepunchcard.com/scripts/punch.php", true); 
xhr.onreadystatechange = function() { ... } 

xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
xhr.setRequestHeader("Content-length", str.length); 

xhr.send(str);