Ajax不发布数据,Chrome扩展

时间:2022-08-26 21:32:21

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:

在我的Chrome扩展中,我可以找到我的“穿孔”。php的文件并接收数据返回,问题是我似乎无法将数据传递给文件。这是我的代码:

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.

这是一个复制了所有相同代码的普通web应用程序。身份证上写着“打孔”。并且是SQL语句的一部分。该语句在Chrome扩展中失败,并返回一个错误语句,因此我开始回显实际的SQL查询,以查看它试图做什么。结果是缺少两个Id的SQL查询,因此出现了错误和失败查询。

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?

这里有语法问题吗?是否有可能,尽管我有谷歌所说的权限设置,我的扩展只能接收数据而不能发送?这里还有其他的愚蠢行为吗?

2 个解决方案

#1


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"

“你确定你有适当的权限在你的扩展中发布数据到那个文件?”我刚刚做了一个简单的片段,从responseText返回“1”。据我所知,这段代码很好,只要它运行在扩展页面(而不是内容脚本)——Mohamed Mansour Jan 9 at 23:45”

Apparently this is the answer.

显然这就是答案。

#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);

#1


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"

“你确定你有适当的权限在你的扩展中发布数据到那个文件?”我刚刚做了一个简单的片段,从responseText返回“1”。据我所知,这段代码很好,只要它运行在扩展页面(而不是内容脚本)——Mohamed Mansour Jan 9 at 23:45”

Apparently this is the answer.

显然这就是答案。

#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);