【问题标题】:Is it possible to perform a Cross-Origin XMLHttpRequest from a content script in Chrome?是否可以从 Chrome 中的内容脚本执行跨域 XMLHttpRequest?
【发布时间】:2017-04-23 01:37:06
【问题描述】:

Cross-Origin XMLHttpRequest 的文档使我看起来应该能够从通过 https 加载的页面访问 http 资源,只要我使用正确的权限。然而,我在尝试此操作时收到以下错误消息。

content.js:1 Mixed Content: The page at 'https://www.example.com/' was loaded over HTTPS, but requested an insecure resource 'http://www.example.com/'. This request has been blocked; the content must be served over HTTPS.

清单:

{
  "name": "Test Extension",
  "version": "0.1",
  "permissions": [
    "http://www.example.com/*",
    "https://www.example.com/*",
  ],
  "content_scripts": [
    {
      "matches": [
        "http://www.example.com/*",
        "https://www.example.com/*"
      ],
      "js": ["content.js"],
      "run_at": "document_start"
    }
  ],
  "manifest_version": 2
}

content.js:

fetch('http://www.example.com/').then(response => {
    console.log('Done!')
});

【问题讨论】:

    标签: javascript google-chrome google-chrome-extension xmlhttprequest cross-domain


    【解决方案1】:

    您的问题不是 CORS。它是混合内容,即使在扩展中也无法解决。

    您可以将您的请求移动到后台页面(通过消息传递)。

    【讨论】:

    • 或者,如果您拥有“example.com”,请考虑将端点托管在 https 上。如果你不拥有它,你可以通过你自己的 https 服务器代理它。
    猜你喜欢
    • 1970-01-01
    • 2014-03-04
    • 2021-05-22
    • 2012-03-14
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 2011-10-06
    相关资源
    最近更新 更多