【发布时间】:2022-01-19 14:41:20
【问题描述】:
我正在尝试使用 dom.fetch(或 dom.Fetch.fetch)api 而不是 Ajax.post,但遇到了一些问题:
这是从 ajax 到 fetch 的正确翻译吗?
Ajax.post(
url = "http://localhost:8080/ajax/myMethod",
data = byteBuffer2typedArray(Pickle.intoBytes(req.payload)),
responseType = "arraybuffer",
headers = Map("Content-Type" -> "application/octet-stream"),
)
dom.fetch(
"http://localhost:8080/fetch/myMethod",
new RequestInit {
method = HttpMethod.POST
body = byteBuffer2typedArray(Pickle.intoBytes(req.payload))
headers = new Headers {
js.Array(
js.Array("Content-Type", "application/octet-stream")
)
}
}
)
虽然在 js 端抛出“ReferenceError: fetch is not defined”,但如果替换为dom.Fetch.fetch,则相同。
我的设置:
新鲜的 jsdom 19.0.0 与
npm init private
npm install jsdom
项目/plugins.sbt
libraryDependencies += "org.scala-js" %% "scalajs-env-jsdom-nodejs" % "1.1.0"
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.8.0")
build.sbt(在 js 项目中)
libraryDependencies += "org.scala-js" %%% "scalajs-dom" % "2.0.0"
jsEnv := new JSDOMNodeJSEnv(JSDOMNodeJSEnv.Config()
.withArgs(List("--dns-result-order=ipv4first")))
认为 Scala.js 1.8 不需要 jsEnv 解决方法(请参阅 https://github.com/scala-js/scala-js-js-envs/issues/12#issuecomment-958925883)。但是当我运行 ajax 版本时仍然需要它。通过解决方法,我的 ajax 版本可以正常工作,因此我的节点安装似乎很好。
【问题讨论】: