Javascript XMLHTTPREQUEST chrome扩展不工作

Javascript XMLHTTPREQUEST chrome扩展不工作,javascript,php,ajax,google-chrome,google-chrome-extension,Javascript,Php,Ajax,Google Chrome,Google Chrome Extension,我无法使用XMLHTTPREQUEST从Chrome扩展后台脚本发送数据,我已经启动并运行了wampserver,我还尝试了外部链接,如google 它做什么: 用户进入权限定义选项卡,后台脚本等待热处理 键,当按下时,启动内容脚本并生成字符串, 字符串被发送回后台脚本,然后返回后台脚本 脚本应该接收字符串并将其发送到一个php文件,即php 文件应该打印hello,这很简单,只是想看看 问题是,以后php将有更多的代码 但它完全不起作用 更新 我尝试打包扩展,然后通过拖放运行它,但没有成功 启

我无法使用XMLHTTPREQUEST从Chrome扩展后台脚本发送数据,我已经启动并运行了wampserver,我还尝试了外部链接,如google

它做什么:

用户进入权限定义选项卡,后台脚本等待热处理 键,当按下时,启动内容脚本并生成字符串, 字符串被发送回后台脚本,然后返回后台脚本 脚本应该接收字符串并将其发送到一个php文件,即php 文件应该打印hello,这很简单,只是想看看 问题是,以后php将有更多的代码

但它完全不起作用

更新

我尝试打包扩展,然后通过拖放运行它,但没有成功 启动php脚本

我尝试卸载chrome,重新启动,然后再次安装,但是 没有运气

我还允许——允许从文件访问文件

chrome.commands.onCommand.addListener(function(command) {
 if (command === "toggle-feature") { 
 chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
     for(var i = 0; i<tabs.length;i++) {
           chrome.tabs.executeScript(tabs[i].id, {"file": "content_script.js"});
     }
   });
  }
}); 

chrome.runtime.onMessage.addListener(
  function(message, sender, sendResponse) {

    var xhttp = new XMLHttpRequest();
    xhttp.open("POST", "http://localhost/test/test.php");
    xhttp.send(message.url);

  });
var url = 'this is just test' ;
chrome.runtime.sendMessage({ 'url' : url });
更新2

我在调试模式下收到以下错误:

扩展::sendRequest:41:未捕获的TypeError:无法读取未定义的{TypeError:无法读取的属性'callback' 未定义

Manifest.json

{

  "manifest_version": 2,
  "name": "Extractor",
  "version": "1",


  "description": "Extract from 144",
  "icons": { "16": "logo16.png",
           "48": "logo48.png",
          "128": "logo128.png" },


        "page_action": {
          "default_icon": {                    
            "16": "logo16.png",           
            "48": "logo48.png",           
            "128": "logo128.png"            
          },
          "default_title": "Extractor"          
        },

  "background": {

    "scripts": ["background.js"],
    "persistent": true
  },
  "content_scripts": [
    {
      "matches" : ["https://www.msn.com/*"],
      "js" : ["content_script.js"]
    }
  ],
 "permissions": [
    "tabs",
    "https://www.msn.com/*",
    "activeTab",
     "http://localhost/*"

  ],
  "commands": {
           "toggle-feature": {
            "suggested_key": {
              "default": "Ctrl+Shift+1",
              "windows": "Ctrl+Shift+2"
            },

            "description": "Extract now"
          }
        } ,
"web_accessible_resources": ["content_script.js"]

}
Background.js

chrome.commands.onCommand.addListener(function(command) {
 if (command === "toggle-feature") { 
 chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
     for(var i = 0; i<tabs.length;i++) {
           chrome.tabs.executeScript(tabs[i].id, {"file": "content_script.js"});
     }
   });
  }
}); 

chrome.runtime.onMessage.addListener(
  function(message, sender, sendResponse) {

    var xhttp = new XMLHttpRequest();
    xhttp.open("POST", "http://localhost/test/test.php");
    xhttp.send(message.url);

  });
var url = 'this is just test' ;
chrome.runtime.sendMessage({ 'url' : url });
test.php

echo "hello";

您只能从chrome扩展向您在
manifest.json
中定义的URL发出XHR请求。在您的代码中,您应该添加
http://localhost
到您的
manifest.json

 "permissions": [
    "tabs",
    "https://www.msn.com/*",
    "activeTab",
     "*://*/*",
     "http://localhost/"

  ],
此权限
“*://*/*”,
无效。必须指定协议(http或https)


更多信息:


您可能希望签入您的代码,以确定是否存在自Chrome 33以来已被降级的代码。请改用

除此之外,还声明,如果您只需要向扩展的另一部分发送一条消息(并且可以选择返回响应),则应使用简化的runtime.sendMessage或tabs.sendMessage

并且,从内容脚本发送请求如下所示:

chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
  console.log(response.farewell);
});
在接收端,您需要设置一个
runtime.onMessage
事件侦听器来处理消息

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    console.log(sender.tab ?
                "from a content script:" + sender.tab.url :
                "from the extension");
    if (request.greeting == "hello")
      sendResponse({farewell: "goodbye"});
  });
此外,有人指出:

sendResponse
回调仅在同步使用时有效,或者如果事件处理程序返回true以指示它将异步响应时有效。如果没有处理程序返回true或者
sendResponse
回调被垃圾收集,则将自动调用
sendMessage
函数的回调


有关如何发送请求和设置事件侦听器的更多信息,您可能需要检查。

添加了警报。它可以工作,但php文件不支持操作系统。我想xmlhttprequest有问题