简体   繁体   中英

xmlHttpRequest via Google Chrome Extension, simple POST to .php page

I've spent hours trying to figure this out.

I have a basic Google Chrome Extension that records some data and I want the data to be sent via xmlHttpRequest to an external php page (with POST).

In my permissions page (manifest.json), I have:

"permissions": [
    "tabs",
    "http://www.mywebsite.com/",
    "https://www.mywebsite.com/",
    "http://*/",
    "https://*/*"
],

In my content script, I have the code:

if (var1 && var2) {
    var xmlhttp = null;

    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    var url = "http://www.mywebsite.com/datalogger.php";
    var params = "var1="+var1+"var1="+var1;
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.open("POST", url, true);
    xmlhttp.send(params);
}

However (you guessed it!) my code won't work. Can anyone figure out what's wrong with it?

http://www.mywebsite.com/ is a match pattern that matches only the root part of the domain (ie, the path " / "). You should use http://www.mywebsite.com/* with a final star instead. You should apply that final to star to all of your host permissions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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