【问题标题】:How to send Json as request to controller file in play framework如何将 Json 作为请求发送到播放框架中的控制器文件
【发布时间】:2014-11-02 17:19:02
【问题描述】:

我想通过 ajax 发送 json 请求,同样我想从控制器中解析。请在下面找到我的代码(查看部分)

<input type="checkbox" id="selectall" />
<table id="tbl" border="1">
  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>2</td>
    <td>3</td>
    <td>5</td>
  </tr>
</table>
<input type="button" id="save" value="save" />
<div id="log"></div>

JQuery 部分

 $("#save").click(function() {       
          //get all the checked checboxex
          $('#tab input:checkbox:checked').each(function() {
            //for each checked checkbox, iterate through its parent's siblings
            var array = $(this).parent().siblings().map(function() {
              return $(this).text().trim();
            }).get();
            //to print the value of array
            console.log(JSON.stringify(array))
             $.ajax({
                 type : 'POST',
                 url : '@routes.Application.save()',
                 dataType: "json",
                 data: JSON.stringify(array),
                 success : function(data) {
                    console.log("success"); 

               },
                 error : function() {

                     alert("failure")
                 }

                 });
          })

控制器部分

public static Result saveboxes()
  {
      JsonNode json = null;
      try{
          DynamicForm dynamicForm = new DynamicForm();
            dynamicForm = dynamicForm.bindFromRequest();
            Dynamic dynamic = dynamicForm.get(); 
            json = Json.toJson(dynamic.getData());
            //String data = json.get("data").asText();
            JsonNode jsonData = Json.parse(json.asText());
         logger.info(jsonData.asText());
      }
      catch(Exception e)
      {
          e.printStackTrace();
      }


        return null;
  }

我收到空指针异常。该视图将根据用户通过复选框的选择返回多行值。如果用户选择三行,输入将是 ["2","3","4"]["2","3","4"]["2","3","5"]。我需要获取控制器中的所有值。如果你建议比这更好的方法。会很棒的。

【问题讨论】:

  • 考虑使用 Play 的 JavaScript 路由支持。您可以为控制器生成 JS 路由器并使用它们,而不是使用 jQuery AJAX 支持——这也将处理 HTTP 方法类型。

标签: jquery ajax json playframework


【解决方案1】:

我检查了您发送的数据格式,它不是 json 格式。 它应该是这样的:

{
   [
      "2",
      "3",
      "4"
   ],
   [
      "2",
      "3",
      "4"
   ],
   [
      "2",
      "3",
      "5"
  ]
}

【讨论】:

  • 你能帮我做同样的事情吗?
【解决方案2】:

仅指定“dataType”是不够的。您还需要设置“contentType”。尝试将contentType: "application/json; charset=utf-8" 添加到您的请求中。

它应该看起来像这样:

             $.ajax({
             type : 'POST',
             url : '@routes.Application.save()',
             contentType: "application/json; charset=utf-8,
             dataType: "json",
             data: JSON.stringify(array),
             success : function(data) {
                console.log("success"); 

           },
             error : function() {

                 alert("failure")
             }

             });

如果您不使用 UTF-8,请确保选择正确的字符集。

【讨论】:

    【解决方案3】:

    在脚本部分之前定义 jsRouter,之后你可以像下面这样使用它:

    @scripts = {
          @helper.javascriptRouter("jsRouter")(
            controllers.routes.javascript.Application.save
          )
           <script type="application/javascript">
    
                       ...
    
                       $("#save").click(function() { 
                              ...
                              jsRouter.controllers.Application.save().ajax({
                                      cache: false,
                                      method: "POST",
                                      contentType: "application/json",
                                      data: JSON.stringify({
                                        ...
                                      }),
                                      success: function(data) {
                                        ...
                                      },
                                      error: function(data) {
                                        ...
                                      }
                               });
                       }
           </script>
    }
    

    【讨论】:

      猜你喜欢
      • 2018-03-05
      • 1970-01-01
      • 1970-01-01
      • 2018-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-05
      • 2021-02-11
      相关资源
      最近更新 更多