如何使用 Scala sttp FetchBackend 处理 html 中的 JavaScript?

How can I use the Scala sttp FetchBackend for handling JavaScript in html?

我需要在 HTML 响应中执行 JavaScript。 我正在使用 sttp 版本 1.5.12。 根据文档,我只需要包含 implicit val sttpBackend = FetchBackend() 但它不起作用。 请参阅文档:https://sttp.readthedocs.io/en/latest/backends/javascript/fetch.html

已经包含了对 Maven 的依赖。

<!-- https://mvnrepository.com/artifact/com.softwaremill.sttp/core -->
<dependency>
    <groupId>com.softwaremill.sttp</groupId>
    <artifactId>core_2.12</artifactId>
    <version>1.5.12</version>
</dependency>

示例:

import com.softwaremill.sttp._
implicit val sttpBackend = FetchBackend()

我希望像其他支持的后端一样使用它。 Eclipse 报告 not found : value FetchBackend

感谢任何帮助。

FetchBackend 是 fetch api 的包装器,它是浏览器 API。您只能将它与 scala-js 一起使用。 如果你打开你的 link 你会注意到 sttpsbt DSL 中的依赖使用三个 %,这意味着它是为 scala-js 编译的版本:

"com.softwaremill.sttp" %%% "core" % "1.5.12"

使用 maven 依赖项,您引用 jvm 版本的 sstp,其中不包含 scala-js 特定后端。

您将不得不为 jvm 使用另一个后端,例如 akka-http-backend.