1

In my Chrome Extension I'm able to get to my 'punch.php' file and receive data back, the problem is that I don't seem to be able to pass data to the file. Here's my code:

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

This is a duplication of a normal web application with all of the same code. The Id's post to 'punch.php' and are a part of an SQL statement. The statement was failing with the Chrome extension, and returning an error statement, so I began echoing out the actual SQL query to see what it was trying to do. The result was the SQL query with the two Id's missing, hence the error and failed query.

Do I have a syntax problem here? Is it possible that though I have the permissions set the way Google says I should, that my extension can only receive data and not send? Is there some other foolishness going on here?

d2burke
  • 4,081
  • 3
  • 39
  • 51
  • `str` is never used. Is that intentional? Where are you trying to pass data in the code example? – bzlm Jan 09 '11 at 22:20
  • 1
    Are you sure you have proper permissions in your extensions to post data to that file? I just did a simple snippet that I return "1" from responseText. As far as I can see, the snippet is good, as long as it runs in the extension page (not the content script) – Mohamed Mansour Jan 09 '11 at 23:45
  • @Mohamed - That was it. Post this as an answer and I'll accept it – d2burke Jan 11 '11 at 04:04

2 Answers2

2

"Are you sure you have proper permissions in your extensions to post data to that file? I just did a simple snippet that I return "1" from responseText. As far as I can see, the snippet is good, as long as it runs in the extension page (not the content script) – Mohamed Mansour Jan 9 at 23:45"

Apparently this is the answer.

OneT
  • 36
  • 2
1

Try adding Content-type and Content-length headers:

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);
serg
  • 109,619
  • 77
  • 317
  • 330