【问题标题】:XMLHttpRequest Cannot Load https://[website].com Cross Origin Requests are not supportedXMLHttpRequest 无法加载 https://[website].com 不支持跨域请求
【发布时间】:2017-07-23 03:38:33
【问题描述】:

我在我的 MacBook(使用 macOS)上成功地使用 phantomjs --web-security=no myfile.js 运行了我的 JavaScript 文件,但未能在 Ubuntu 上运行它,并出现此错误:

XMLHttpRequest Cannot Load https://[website].com Cross Origin
Requests are not supported

我发现我的问题与以下问题不同

  1. XMLHttpRequest cannot load Cross origin requests are only supported for HTTP
  2. XMLHttpRequest cannot load https://www.[website].com/

对于第一个,我的错误消息只是“不支持”,但没有告诉我“仅支持 HTTP”(尽管我将请求发送到 HTTPS url)。 对于第二个,我的错误消息不需要“Access-Control-Allow-Origin”标头。

我尝试了 PhantomJS 的 --debug=true 选项,结果发现当在 Ubuntu 中运行时,PhantomJS 会默默地丢弃 cookie。

在我的情况下,谁能告诉我如何解决这个问题?

【问题讨论】:

  • 实际的父请求是什么?子请求报错,可能是父子请求的协议不同。
  • @ThanNgoHoai 我的请求只是var request = new XMLHttpRequest(),然后是request.open('GET','my URL',false); request.sent()。我不知道什么是子请求。
  • 所以您的“我的网址”应该与当前页面的协议和服务器名称相同。
  • 是的,我认为它们是相同的,否则它无法在我的 macOS 中运行而不会出现此错误。
  • 我在 phantomjs 中尝试了--debug=true 选项。事实证明,当在 Ubuntu 中运行时,PhantomJS 会默默地丢弃 cookie。恐怕是这个错误的触发因素?

标签: javascript xmlhttprequest phantomjs


【解决方案1】:

phantomjs 似乎有一个选项可以让 XMLHttpRequests 从本地文件发送到 http 服务器:--local-to-remote-url-access

供参考:http://phantomjs.org/api/command-line.html

【讨论】:

    猜你喜欢
    • 2012-06-27
    • 2015-11-17
    • 2014-07-04
    • 2012-01-16
    相关资源
    最近更新 更多