Play框架中的AJAX帖子

时间:2015-02-04 20:30:32

标签: ajax playframework playframework-2.0 playframework-2.2

您好我正在尝试在播放框架中向我的方法发送一个字符串,但在单击提交按钮后,我的控制台出现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) ; }

请你帮帮我。

1 个答案:

答案 0 :(得分:0)

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