【问题标题】:Ajax Post Call Play 2 Framework 2.6.19 send always None to ControllerAjax Post Call Play 2 Framework 2.6.19 始终向控制器发送无
【发布时间】:2018-10-16 14:11:05
【问题描述】:

我正在尝试将旧的 Play 2 Framework 应用更新到最新版本 2.6.19

一个视图使用 Post Query Ajax 将表单发送到控制器,但现在更新代码后,表单中绑定的数据始终为空(“无”)。

如果我使用文档并编写 ScalaForm,数据会到达,但我不知道是否可以将脚本与 Scala Form Helpers 混合使用,因为我需要在提交之前和之后执行一些操作。

我怀疑问题可能是过滤器或application.conf中的某些东西,无法成功绑定数据。

我使用的是 jquery 1.11.2

这是我在旧版本中使用但现在不起作用的代码。

观点:

<form method="post" id="entrarLTerme">
<h4>Término Actual:</h4>
<p class="redex" contenteditable="true" id="ent" spellcheck="false"></p>
<button id="accepta" type="submit" value="val">@messages.messages("tilcgfs.entrar")</button>
</form>

<script>
    $("#entrarLTerme").submit(function (e) {
                uncheck();
                var formURL = $(this).attr("action");
                var aux = $("#ent").text();
                $.ajax(
                        {
                            url: formURL,
                            type: "POST",
                            data: {valor: aux, op: "0", pag: "2"},
                            success: function (data) {
                                // Call some JS functions
                                }
                            },
                            error: function () {
                                // Call some JS functions
                            }
                        });
                e.preventDefault();
            });
</script>

和控制器:

class TilcWT @Inject()(component: ControllerComponents, instanciesTilcWT: InstanciesTilcWT,langs: Langs) extends AbstractController(component) with I18nSupport {

  implicit var messages: Messages = MessagesImpl(Lang("ca"), messagesApi)

  val opcionsDefinicions = Form(
    tuple(
      "valor" -> text,
      "op" -> text,
      "pag" -> text))

  def opcions = Action { implicit request =>
    val usuari: String = request.session.get("user").get
    opcionsDefinicions.bindFromRequest.fold(
      formWithErrors => {
        BadRequest("Not Alloweddd")
      },
      options => {
        val valor = options._1
        val opt = options._2
        val page = options._3
        BadRequest("Not Alloweddd")
      }
    )
    BadRequest("Not Allowed")
  }

}

在我使用折叠功能之前,我“得到”了这个,没有任何问题。调试器正确到达“opcions”方法,当然 BadRequest 是虚拟的。

我的 application.conf 如下所示:

# The application languages
# ~~~~~
play.i18n.langs=["en","ca","es"]

play.filters.enabled=[]

提前致谢

【问题讨论】:

  • 日志中有什么可以帮助理解的吗?

标签: ajax playframework-2.0


【解决方案1】:

由于CSRF protection 这件事,我在 Play 2.6 中的帖子出现问题。

要检查您的问题是否与之相关,请首先尝试停用此 CSRF 过滤器,如下所示:How can I disable the CSRF filter on Play 2.6?

那么你当然必须使用 Play 助手将 CSRF 添加到你的 Post 和 URL 请求中,例如:

@form(routes.ItemsController.save()) {
    @CSRF.formField
    . ..
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    相关资源
    最近更新 更多