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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 模型在y分布之外的数据上预测能力不好如何解决
    • ¥15 processing提取音乐节奏
    • ¥15 gg加速器加速游戏时,提示不是x86架构
    • ¥15 python按要求编写程序
    • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
    • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
    • ¥15 opencv图像处理,需要四个处理结果图
    • ¥15 无线移动边缘计算系统中的系统模型
    • ¥15 深度学习中的画图问题
    • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条