0

我正在使用jQuery Datatable插件,我想要获取在我的控制器操作中发送到服务器的默认参数,如链接所示。播放获取json请求参数

这是我的AJAX请求码

$(document).ready(function() { 
     $('#example').DataTable({ 
      "processing": true, 
      "serverSide": true, 
      "ajax": { 
       "url": "getTable", 
       "type": "POST" 
      } 
     }); 
    }); 

这是我的控制器操作代码

public Result ajaxDisplayTable() { 
     Logger.info("This is just another method for ajax post action call..."); 
     String userAgent = request().getHeader("User-Agent"); 
     Logger.info("user agent = "+ userAgent); 
     RequestBody body = request().body(); 
     Logger.info("bare body = "+ body); 
     Logger.info("json ... "+ body.asJson()); 
     Logger.info("body as json = " + body.asText()); 
     return ok("Got json: "); 
} 

请求被发送到服务器,并且操作方法被称为体被印刷,但身体。 asJson()和body.asText()总是为null,如下图所示。在下面的图像显示 enter image description here

而作为 enter image description here enter image description here

外观和数据作为应用程序/ JSON传递的要求,这里纠正我,如果我错了,那么为什么body.asJson()为null,如何在操作方法中获取所有请求参数?我正在使用Play 2.4.2版本(Damiya)。

+1

请参阅'Content-type',它是'x-www-form-urlencoded',它不是JSON。也许你应该使用'body.asFormUrlEncoded()'来代替,请参阅[Default body parser:AnyContent](https://www.playframework.com/documentation/2.4.x/JavaBodyParsers#Default-body-parser:- AnyContent)? –

+0

什么是接受:application/json? – srk

+0

这是浏览器接受的响应,请参阅[接受](http://www.w3.org/Protocols/HTTP/HTRQ_Headers.html#z3)。 –

回答

1

您的请求发送为application/x-www-form-url-encoded,请参阅Content-type标题。您需要使用body.asFormUrlEncoded()而不是body.asJson()

public Result ajaxDisplayTable() { 
    RequestBody body = request().body(); 
    final Map<String, String[]> values = body.asFormUrlEncoded(); 
    final String valDraw = values.get("draw")[0]; 
} 

请参阅Body parsers了解更多信息。