scala.js在客户端表单处理/访问scala.js客户端上的表单

时间:2017-01-03 17:45:19

标签: scala.js

我想提交一个表单,并希望向用户显示带有微调器的进程并重新加载新信息。

@JSExport
  def addToCart(form: html.Form): Unit = {
    form.onsubmit = (e: dom.Event) => {
      e.preventDefault()
    }
    val waitSpan = span(
      `class` := Waiting.glyphIconWaitClass
    )
    val waiting = form.getElementsByTagName("button").head.appendChild(waitSpan.render)
    dom.window.alert(JSON.stringify( form.elements.namedItem("quantity") ))
    Ajax.InputData
    Ajax.post(form.action,withCredentials = true).map{q =>
      //
    }
  }

我无法访问表单数据。此外,我无法执行ajax调用来证明表单并执行它。我找不到办法了。有人有想法吗?

1 个答案:

答案 0 :(得分:0)

jQuery有帮助。我现在用它们来序列化表单。但现在我不再具有使用bindOfRequest()

播放表单的能力
val jForm = $("#"+form.id)
val serialized = jForm.serialize()
Ajax.post(s"/js/api/form/${UUID.randomUUID().toString}",withCredentials = false,timeout = 12000,data = serialized,headers = Map("X-CSRFToken"->"nocheck","Csrf-Token"->"nocheck"))

我总是这样:

occurrence%5B%5D=400g&quantity=1&csrfToken=c1606da9a261a7f3284518d4f1fd63eaa8bbb59e-1483472204854-1c5af366c62520883474c160

但现在我不知道自己要做什么。遗憾。

def executeAddToCartForm(articleId: UUID) = silhouette.UserAwareAction.async{implicit req =>
    val form = complexCartForm.bindFromRequest()
    Try(form.get) match {
      case Success((i,seq)) => println("article: " + i)
      case _ => println(form.errors.mkString + " " + req.body.asText + " " + URLDecoder.decode(req.body.asText.get,"UTF-8"))
    }
    Future.successful(Ok("danke"))
  }

总是失败:(我会看看反应。

ADDED

有时我需要更多的睡眠!

Ajax.post(
      url = form.action,
      withCredentials = true,
      timeout = 12000,
      data = serialized,
      headers = Map("Content-Type" -> "application/x-www-form-urlencoded")
    )

有这个:headers = Map(“Content-Type” - >“application / x-www-form-urlencoded”)我通常可以使用bindFromRequest():)

咖啡我需要更多