错误:在 Ajax 调用 ScalaJS 上禁止跨源本地主机

问题描述

我正在尝试在维基百科上执行一个简单的“GET”:

Ajax.get("https://en.wikipedia.org/w/index.PHP?title=Special:Random") map (_.responseText)

当我尝试运行时出现此错误

[error] org.scalajs.jsenv.ExternalJSRun$NonZeroExitException: exited with code 1
[error]         at org.scalajs.jsenv.ExternalJSRun$$anon$1.run(ExternalJSRun.scala:186)
[error] stack trace is suppressed; run 'last Compile / run' for the full output
[error] (Compile / run) org.scalajs.jsenv.ExternalJSRun$NonZeroExitException: exited with code 1
[error] Total time: 3 s,completed Mar 4,2021,4:47:42 PM
Error: Cross origin http://localhost forbidden
    at dispatchError (C:\Users\mjwin\IdeaProjects\wiki-brain\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:62:19)
    at Object.validCORSHeaders (C:\Users\mjwin\IdeaProjects\wiki-brain\node_modules\jsdom\lib\jsdom\living\xhr\xhr-utils.js:74:5)
    at Request.<anonymous> (C:\Users\mjwin\IdeaProjects\wiki-brain\node_modules\jsdom\lib\jsdom\living\xhr\XMLHttpRequest-impl.js:673:25)
    at Request.emit (events.js:315:20)
    at Redirect.onResponse (C:\Users\mjwin\IdeaProjects\wiki-brain\node_modules\request\lib\redirect.js:147:11)
    at Request.onRequestResponse (C:\Users\mjwin\IdeaProjects\wiki-brain\node_modules\request\request.js:986:22)
    at ClientRequest.emit (events.js:315:20)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:641:27)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:126:17)
    at TLSSocket.socketonData (_http_client.js:509:22) {
  type: 'XMLHttpRequest'
}

我已经四处寻找有关此错误的信息,但我仍然不确定它的含义。我想我知道我遇到了 CORS 问题,但我不知道该怎么办。

解决方法

您需要允许将文件读入 URL:

Cross-Origin Read Blocking (CORB) 阻止了跨源响应

不允许您的网址访问。您的脚本出错了。