Javascript 从Chrome扩展访问API

Javascript 从Chrome扩展访问API,javascript,php,google-chrome-extension,Javascript,Php,Google Chrome Extension,我有一个网站,我已经作为谷歌浏览器的扩展添加。我需要的是,我必须从扩展名访问服务器文件。我需要的是,无论服务器文件输出什么,我都需要访问扩展名中的输出。 这是我的manifest.json: { "name": "Calpine Extension", "version": "1.0", "description": "Log on to calpinemate", "manifest_version": 2, "browser_action": {

我有一个网站,我已经作为谷歌浏览器的扩展添加。我需要的是,我必须从扩展名访问服务器文件。我需要的是,无论服务器文件输出什么,我都需要访问扩展名中的输出。 这是我的manifest.json:

{
    "name": "Calpine Extension",
    "version": "1.0",
    "description": "Log on to calpinemate",
    "manifest_version": 2,
    "browser_action": {
        "default_icon": "icon_128.png"
    },
    "background": {
        "persistent": false,
        "scripts": ["background.js"]
    },

    "browser_action": {
        "default_title": "Test Extension",
        "default_icon": "calpine_not_logged_in.png"
    },

    "externally_connectable": {
        "matches": ["http://calpinemate.com/"]
    }
}
这是我的background.js

chrome.browserAction.onClicked.addListener(function (tab) {
    chrome.tabs.create({ "url": "http://calpinemate.com" });
});
   function getGmailUrl() {
   return "http://calpinemate.com/";
   }
   function isGmailUrl(url) {

      return url.indexOf(getGmailUrl()) == 0;
      }
   chrome.browserAction.onClicked.addListener(gotopage);

      function gotopage(){    
      chrome.tabs.query({
       url: "http://calpinemate.com/*",
      currentWindow: true
     }, function(tabs) {
    if (tabs.length > 0) {
        var tab = tabs[0];
        console.log("Found (at least one) Gmail tab: " + tab.url);
        console.log("Focusing and refreshing count...");
        chrome.tabs.update(tab.id, { active: true });
         updateIcon();
    } else {
        console.log("Could not find Gmail tab. Creating one...");
        chrome.tabs.create({ url: getGmailUrl() });
         updateIcon();
    }
      });
    }

        if (chrome.runtime && chrome.runtime.onStartup) {
      chrome.runtime.onStartup.addListener(function() {

       updateIcon();
      });
      } else {
        chrome.windows.onCreated.addListener(function() {

        updateIcon();
           });
         }
     function updateIcon(){

   var req = new XMLHttpRequest();
     req.addEventListener("readystatechange", function() {
         if (req.readyState == 4) {
    if (req.status == 200) {
        localStorage.item=req.responseText;
        //alert(localStorage.item);
        if(localStorage.item==1){
          chrome.browserAction.setIcon({path:"calpine_logged_in.png"});
          chrome.browserAction.setBadgeBackgroundColor({color:[190, 190, 190, 230]});
          chrome.browserAction.setBadgeText({text:""});   
        }
        else{
        chrome.browserAction.setIcon({path:"calpine_not_logged_in.png"});
        chrome.browserAction.setBadgeBackgroundColor({color:[190, 190, 190, 230]});
        chrome.browserAction.setBadgeText({text:""}); 
        }
        // We received the data
        //alert("Data: " + req.responseText);
      } else {
        // Handle the error
        alert("ERROR: status code " + req.status);
      }
    }
   });
       req.open("GET", "http://blog.calpinetech.com/test/index.php", true);
      req.send(null);
现在我需要的是我有一个输出1的服务器文件(index.php)。现在我需要的是,我想得到这个1的扩展。这就是文件输出的内容,我希望以扩展名输出。我该怎么做? 这是我的index.php

<?php echo 1 ;?>

}首先请求访问服务器的权限:

// In manifest.json
...
permissions: [
   ...
   "*://calpinemate.com/index.php"
],
然后使用AJAX访问数据:

var req = new XMLHttpRequest();
req.addEventListener("readystatechange", function() {
    if (req.readyState == 4) {
        if (req.status == 200) {
            // We received the data
            alert("Data: " + req.responseText);
        } else {
            // Handle the error
            alert("ERROR: status code " + req.status);
        }
    }
});
req.open("GET", "https://calpinemate.com/index.php", true);
req.send(null);

嗨..它显示错误:状态代码0..会出现什么问题??php在一个名为test的文件夹中。所以我尝试了req.open(“GET”,”,true)。但也出现了相同的错误。原因可能是什么?请帮助我它显示错误:状态代码404。请帮助我。我想不出来,请检查我的问题,我已经包括了我的索引。phpHi谢谢你,它工作得很好。我非常感谢你。我还有一个疑问。这些操作是在我单击扩展图标时发生的。我们如何才能在启用扩展时强制执行这些操作?我的意思是,不单击扩展图标,它应该访问index.php。这是怎么可能的?请帮助我您希望什么时候执行操作?“启用扩展时”是什么意思?