【问题标题】:AJAX post in Play FrameworkPlay Framework 中的 AJAX 帖子
【发布时间】:2015-02-04 20:30:32
【问题描述】:

您好,我正在尝试在 play 框架中向我的方法发送一个字符串,但是在单击提交按钮后,我的控制台中出现 500(内部服务器错误)错误。这是我的前端代码:

 $("#submit").click( function() {
 $.ajax({
    url: "@routes.Application.upload()",
    data: JSON.stringify(filename),
    headers: {
      'Content-Type': 'application/json'
    },
    type: 'POST',
    success: function(res) {
      if (res) {
        console.log("Success!");
      } else {
        console.log("Failed...");
      }
    }
  });

   return false;
});

我的表格:

    <form id="#idForm">
            <input type="file" name="file" id="file">
            <input id="submit" type="submit">
        </form>

这是我的上传方法

 public static Result upload() {
    JsonNode node = request().body().asJson().get("stringField");
    String inputString = node.asText();
    System.out.println(inputString) ; }

你能帮我解决这个问题吗?

【问题讨论】:

  • 您正在尝试将数据发布到函数中? url: "@routes.Application.upload()",
  • @britter @routes.Application.upload() 基本上是 /upload 。我都试过了,它给了我同样的错误POST http://127.0.0.1:9000/upload 500 (Internal Server Error)
  • 你能显示你从服务器得到的错误吗?您可以查看控制台输出,或者如果您使用的是 Chrome 或 Firefox,请在开发工具中检查网络
  • 也尝试用@BodyParser.Of(BodyParser.Json.class)注释你的“上传”方法
  • @Salem 这个游戏控制台错误[error] play - Cannot invoke the action, eventually got an error: java.lang.Null PointerException [error] application - ! @6l4i8c1p2 - Internal server error, for (POST) [/upload] -&gt; play.api.Application$$anon$1: Execution exception[[NullPointerException: null]]

标签: ajax playframework playframework-2.0 playframework-2.2


【解决方案1】:

如果您尝试发送字符串,如何更改表单中的 input type="text" 并使用 serializeArray 序列化表单,外部 ajax 函数(如 var formData = JSON.stringify($("#idForm").serializeArray());)和内部 ajax 函数集 data: formData 以及 contentType : "application/json"。在控制器中的 upload() 内部。 JsonNode formValue=request().body().asJson(); String jsonValue =formValue.findValue("value").textValue(); 检查这是否有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多