Ajax请求-Chrome扩展


Cross-Domain jQuery.Ajax request - Chrome extension

我想实现一个Google Chrome扩展,它将在Ajax请求的结果之后显示一个通知。

我编码了允许创建通知的函数,所以我只需要执行Ajax请求,该请求在属于我的远程服务器上获取一个.php文件。这个请求只是失败了,什么也没发生。尽管如此,当我试图实现从我的服务器到我的服务器的请求(没有扩展)时,没有问题,我从中推断出这是一个"跨域"的问题。。。

以下是manifest.json的重要元素(对于这个问题)(我只是放了所有可能的权限^^):

{
    "background": {
        "scripts": ["myScript.js", "jquery-2.1.4.min.js"]
    },
    "manifest_version": 2,
    "permissions": [ "http://*/", "https://*/" , "http://*/*" , "https://*/*", "tabs", "notifications", "browsingData", "webRequest", "webNavigation" ],
    ...
    ...
}

以下是myScript.js中的AJax请求:(spawnNotification功能运行良好,无需请求即可测试)

$.ajax({
    url: "http://www.domain.com/test/get.php",
    type: "GET",
    crossDomain : true,
    success: function() {
        spawnNotification("Title", "work", "img/notif.png", "http://www.domain.cor/forum/");
    },
    error: function() {
        spawnNotification("Title", "error", "img/notif.png", "http://www.domain.co/forum/");
    }
});

最后,get.php文件:

<?php
header("Content-Type: text/plain");
header("Access-Control-Allow-Origin: *");
$str = 15;
echo $str;
?>

我在这里做错了什么?谢谢

(以下是一些对我没有帮助的话题…

Chrome扩展跨域请求

Chrome扩展xhr跨域请求给出错误:";访问控制允许来源不允许")

您需要提供更多的响应标头,有关详细信息,请参阅跨来源资源共享规范。

以下是服务器代码中所需的伪代码(来自我在这里的另一个答案)(对不起,不要写太多PHP,因此是伪代码):

// Find out what the request is asking for
corsOrigin = get_request_header("Origin")
corsMethod = get_request_header("Access-Control-Request-Method")
corsHeaders = get_request_header("Access-Control-Request-Headers")
if corsOrigin is null or "null" {
    // Requests from a `file://` path seem to come through without an
    // origin or with "null" (literally) as the origin.
    // In my case, for testing, I wanted to allow those and so I output
    // "*", but you may want to go another way.
    corsOrigin = "*"
}
// Decide whether to accept that request with those headers
// If so:
// Respond with headers saying what's allowed (here we're just echoing what they
// asked for, except we may be using "*" [all] instead of the actual origin for
// the "Access-Control-Allow-Origin" one)
set_response_header("Access-Control-Allow-Origin", corsOrigin)
set_response_header("Access-Control-Allow-Methods", corsMethod)
set_response_header("Access-Control-Allow-Headers", corsHeaders)
if the HTTP request method is "OPTIONS" {
    // Done, no body in response to OPTIONS
    stop
}
// Process the GET or POST here; output the body of the response

@T.J.Crowder

谢谢Crowder,我试着用PHP写它,我第一次尝试是为了我的get.PHP:

<?php
header("Content-Type: text/plain");
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: X-Requested-With");
$str = 15;
echo $str;
?>

它不起作用,所以我用你说的话搜索了一下,发现https://stackoverflow.com/a/9866124/5733765

get.php:

<?php
if (isset($_SERVER['HTTP_ORIGIN'])) {
    header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Max-Age: 86400');
}
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
        header("Access-Control-Allow-Methods: GET, POST, OPTIONS");         
    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
        header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
    exit(0);
}
$str = 15;
echo $str;
?>

但仍然不起作用

我发现了问题。。。我们必须使用xhr

myScript.js:

var xhr = new XMLHttpRequest();
xhr.open("GET", "http://domain.com/test/get.php", true);
xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
        alert(xhr.responseText);
    }
}
xhr.send();

谢谢你的帮助;)

EDIT:真正的问题是在myScript.js中定义jquery.jsmanifest.json:

"background": {
        "scripts": ["jquery-2.1.4.min.js", "notification.js"]
},