如何在安全站点上正确地在chrome扩展内容脚本中执行ajax?

时间:2015-07-04 06:18:21

标签: javascript ajax google-chrome google-chrome-extension

我有一个chrome扩展名,在内容脚本文件中我有这样的内容:

$(document).ready(function() {
var currentHostname = cleanHostname(window.location.hostname);

$.ajax({
    url:    BASEURL + 'checkSite',
    dataType: 'json',
    method: 'get',
    data: {'hostname': currentHostname},
    success: function(data) {
        data = JSON.parse(data);
        console.log(data);
    }
});
});

此代码在非https网站上运行良好,因为chrome允许在其上使用非https资源。但是,每当我访问https网站时,我都会得到:

Mixed Content: The page at 'https://www.google.com/' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http://localhost:8000/checkSite?hostname=google.com'. This request has been blocked; the content must be served over HTTPS.

这完全有道理。我理解这个政策。但我认为延期应该得到特殊待遇。我该如何解决这个问题?我已经考虑过使用background.js来做所有的ajax,然后做消息传递,但我非常想避免这种情况,但是如果需要的话,我会的。

有人知道吗?

2 个答案:

答案 0 :(得分:1)

来自Here

  

请求跨域权限

     

权限通过添加主机或主机匹配   清单文件的权限部分的模式(或两者),   扩展可以请求访问其外部的远程服务器   原点。

{
  Transfer-Encoding: chunked
  Date: Fri, 03 Jul 2015 16:15:33 GMT
  Server: Apache-Coyote/1.1
  Content-Type: application/json
}

答案 1 :(得分:-1)

我按照@Brian的建议解决了我的问题。

我刚刚在background.js中完成了所有ajax,并使用消息传递与我的内容脚本进行通信。