gpt4 book ai didi

javascript - Vaadin 12 将对象传递给 JavaScript 的函数 : Can't encode class

转载 作者:行者123 更新时间:2023-11-29 20:41:15 25 4
gpt4 key购买 nike

Vaadin 12,Kotlin 项目

在我的 myPage.html 中我有 javascript:

 myObject.redirectToCheckout({
sessionId: "1111_2222",
}).

所以我需要从 Vaadin 12 调用 javaScript 函数 redirectToCheckout 并传递正确的参数作为对象。所以这是我的 Vaadin 片段:

import com.vaadin.flow.component.dependency.HtmlImport
import com.vaadin.flow.component.dependency.JavaScript
import com.vaadin.flow.component.html.Div
import com.vaadin.flow.router.Route
import com.vaadin.flow.server.VaadinRequest
import java.io.Serializable

@Route(value = "redir")
@HtmlImport("styles/myPage.html")
class RedirectForm : Div() {
init {
val request = VaadinRequest.getCurrent()
val paramGoto = request.getParameter("goto")
val redirect = Redirect("$paramGoto")
UI.getCurrent().getPage().executeJavaScript("myObject.redirectToCheckout($0)", redirect) // **error here**
}

inner class Redirect : Serializable {
var sessionId: String

constructor(sessionId: String) {
this.sessionId = sessionId
}
}
}

但我得到错误:

Caused by: java.lang.IllegalArgumentException: Can't encode class com.myproject.view.RedirectForm$Redirect to json
at com.vaadin.flow.internal.JsonCodec.encodeWithoutTypeInfo(JsonCodec.java:165)
at com.vaadin.flow.internal.JsonCodec.encodeWithTypeInfo(JsonCodec.java:80)
at com.vaadin.flow.component.page.Page.executeJavaScript(Page.java:338)
at com.myproject.view.RedirectForm.<init>(RedirectView.kt:28)
... 50 common frames omitted

最佳答案

使用JsonObject代替Redirect:

val request = VaadinRequest.getCurrent()
val paramGoto = request.getParameter("goto")
val json = Json.createObject()
json.put("sessionId", "$paramGoto")
UI.getCurrent().getPage().executeJavaScript("myObject.redirectToCheckout($0)", json)

关于javascript - Vaadin 12 将对象传递给 JavaScript 的函数 : Can't encode class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55490451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com