Ad
Vaadin 12 Pass Object To JavaScript's Function: Can't Encode Class
Vaadin 12, Kotlin project
In my myPage.html
I has javascript:
myObject.redirectToCheckout({
sessionId: "1111_2222",
}).
So I need to call javaScript function redirectToCheckout
from Vaadin 12 and pass correct param as object.
So here my Vaadin snippet:
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
}
}
}
but I get error:
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
Ad
Answer
Use JsonObject
instead 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)
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad