简体   繁体   English

无法通过Chrome扩展程序的Ajax请求将值存储在数据库中

[英]Unable to store values in database through an Ajax request from chrome extension

I am making an extension which takes the page url and store it in the database when the user submits the button. 我正在做一个扩展,当用户提交按钮时,它接受页面URL并将其存储在数据库中。

I am stuck at this and i think that i am missing something in the extension part which I am unable to identify. 我对此感到困惑,我认为我在扩展部分中缺少无法识别的内容。 Its just a learning project so i haven't included any validations. 它只是一个学习项目,所以我没有进行任何验证。

Here are a few details; 这里有一些细节;

manifest.json 的manifest.json

{
"manifest_version": 2,
"name": "Hello Extention",
"description": "POST details of the current page.",
"version": "0.1",
"content_scripts": [{
  "js": ["contentscript.js"],

}],
"web_accessible_resources": ["script.js"],
"background": {
    "scripts": ["background.js"],
    "persistent": true
},
"browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
},
"permissions": [

    "contextMenus",
    "bookmarks",
    "tabs", 
    "http://*/", 
    "https://*/"
]
}

This is my popup.html 这是我的popup.html

  <input type="text" name="url" id="url" value=""><br>
  <input type="text" name="title" id="title"></br>
  <input type="button" name="submit" value="Go" id="submit"><br>

background.js background.js

chrome.tabs.getSelected(null, function (tab) {
    var tabId = tab.id;
    var tabUrl = tab.url;
    var tabTitle = tab.title;

    document.getElementById("url").value = tabUrl;
    document.getElementById("title").value = tabTitle;

    chrome.browserAction.setBadgeText({
        text: "10+"
    });
});

Now when user clicks on the go button i have called the ajax request 现在,当用户单击“执行”按钮时,我已经调用了ajax请求

$("#submit").click(function () {
    document.getElementById("load").innerHTML = "<img style='height:30px;' src='/img/loading-  sprocket-gray-light-transparent.gif' />";
    var title = document.getElementById("title").value;
    var url = document.getElementById("url").value;
    var xhr = new XMLHttpRequest();
    if (!xhr) {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xhr.open("post", "updatedata.php", true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send("url=" + url + "&title=" + title);
    xhr.onreadystatechange = function () {
        if (xhr.status == 200 && xhr.readyState == 4) {
            reply = xhr.responseText;
            alert(reply);
        }
    }
});

updatedata.php updatedata.php

 <?php
    echo "welcome";
    include('connection.php');
    $url = $_GET['url'];
    $title = $_GET['title'];
    mysql_query("insert into `data` VALUES('','$url','$title');" );
 ?>

Now the alert(reply) returns me the entire Updatedata.php file instead of sending the welcome message. 现在, alert(reply)将向我返回整个Updatedata.php文件,而不是发送欢迎消息。 I also tried executing the code separately (ie not from the extension) there its alerting me welcome and also inserting the values in the database. 我也尝试单独执行代码(即不从扩展名开始),在那里警告我,并在数据库中插入值。 So I think there is nothing wrong with the Ajax request or the PHP file. 因此,我认为Ajax请求或PHP文件没有错。 but I think i am missing something with the extention on how to send request or something 但我想我在如何发送请求方面缺少了一些东西

You cannot use PHP in a Chrome extension. 您不能在Chrome扩展程序中使用PHP。 Put it on a PHP server and send the request to the server. 将其放在PHP服务器上,然后将请求发送到服务器。 And fix the glaring SQL injection vulnerability in your PHP script. 并修复PHP脚本中明显的SQL注入漏洞。

Your PHP script seems to only be used for storing data in a database. 您的PHP脚本似乎仅用于在数据库中存储数据。 Unless you need to access this data through a different view, I suggest to use the client-side storage APIs to persist data, eg with chrome.storage . 除非您需要通过其他视图访问此数据,否则建议使用客户端存储API持久化数据,例如使用chrome.storage

If you need a full-fledged database, take a look at the IndexedDB . 如果您需要完整的数据库,请查看IndexedDB Given your level of knowledge, I suggest to use the chrome.storage API because it is easier to use, especially for novice programmers (no offence intended) 鉴于您的知识水平,我建议使用chrome.storage API,因为它易于使用,尤其是对于新手程序员(不打算冒犯)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM