doumi4676 2015-04-20 12:24
浏览 557

无法从chrome扩展程序发送ajax请求

This is the code that runs after clicking on browser action when I simply paste GET request url in browser window php script works fine but it doesn't work if using extension all other parts of extension are working fine.

chrome.extension.onMessage.addListener(function(request, sender) 
{
  if (request.action == "getSource") 
  { 
    message.innerText = request.source;

    var httpRequest;
    function makeRequest(url)  
    {
        if (window.XMLHttpRequest) 
        { // Mozilla, Safari, ...
            httpRequest = new XMLHttpRequest();
        } 
        else if (window.ActiveXObject) 
        { // IE
            try 
            {
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
            }   
            catch (e) 
            {
                try 
                {
                    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                } 
                catch (e) {}
            }
        }
        if (!httpRequest) 
        {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        httpRequest.onreadystatechange = alertContents;
        httpRequest.open('GET', 'http://example.com/Data.php?fname=Henry', true);
        httpRequest.send();
    }
    function alertContents() 
    {
        if (httpRequest.readyState === 4) 
        {
            if (httpRequest.status === 200) 
            {
                alert(httpRequest.responseText);
            } 
            else 
            {
                alert('There was a problem with the request.');
            }
        }
    }
}
});

function onWindowLoad() {

  var message = document.querySelector('#message');

  chrome.tabs.executeScript(null, {
    file: "getPagesSource.js"
  }, function() {
    // If you try and inject into an extensions page or the webstore/NTP        you'll get an error
    if (chrome.extension.lastError) {
      message.innerText = 'There was an error injecting script : 
' + chrome.extension.lastError.message;
    }
  });

}

window.onload = onWindowLoad;

This is manifest.json file

{
  "name": "Get pages source",
  "version": "1.0",
  "manifest_version": 2,
  "description": "Get pages source from a popup",
  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
  "permissions": ["activeTab", "<all_urls>","http://*/"]
}

edited: This is my php script it works fine on default whenever page is accessed

<?php
$servername = "fdb13.biz.nf";
$username = "1806447_gurbhu";
$password = "ggth0719";
$dbname = "1806447_gurbhu";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$nH=$_GET["fname"];

 //insert data to notesPr
$sql = "INSERT INTO derp (raceId, raceLink)
VALUES ('1', '".$nH."')";
$conn->query($sql);
$conn->close();
?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥40 复杂的限制性的商函数处理
    • ¥15 程序不包含适用于入口点的静态Main方法
    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码