JavaScript 向我的服务器发送 GET 请求失败

JavaScript fails on sending GET requests to my server

我有这个代码:

function httpGetAsync(theUrl, callback)
{
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.onreadystatechange = function() { 
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
            callback(xmlHttp.responseText);
    }
    xmlHttp.open("GET", theUrl, true); // true for asynchronous 
    xmlHttp.send(null);
}

 httpGetAsync("http://myserver.com/test.php/?u=test&p=testtest");

当我尝试在 Firefox 的控制台上 运行 它时,我得到:

NetworkError: A network error occurred.

为什么会这样?我该如何解决这个问题?

如果您正在开发浏览器扩展,则发送 GET 请求

转到您的 manifest.json 文件并为 www.example.com 添加权限:

{
    "name": "My extension",
    ...
    "permissions": [
        "http://www.example.com/*"
    ],
    ...
}

然后在你的后台页面(或其他地方)你可以做:

var xhr = new XMLHttpRequest();
xhr.open("GET", "http://www.example.com/test.php?foo=bar", false);
xhr.send();

var result = xhr.responseText;

参考