4

我在向 Play Controller 发送 json 数据时遇到问题。

seach.scala.html

$.ajax({
            type :  "POST",
            dataType: 'json',
            data: {
                'filter': "John Portella"
            },
            url  :  "@routes.Search.findPag()",
            success: function(data){
                console.log(data);
            }
        });
        return false;

控制器 : POST /find/findPag Search.findPag()

public static Result findPag(){    
   JsonNode json = request().body().asJson();
   return ok();
}

调试我得到 json = null 。您认为可能是哪个问题?感谢。

4

2 回答 2

8

您必须对数据进行字符串化。就像现在一样,我认为这.toString()将在数据对象上被调用,这不是可以在服务器端正确解析为 JSON 的东西。

var d = { 'filter': "John Portella" };
$.ajax({
    type :  "POST",
    dataType: 'json',
    data: JSON.stringify(d),
    url  :  "@routes.Search.findPag()",
        success: function(data){
            console.log(data);
        }
});
于 2013-05-07T06:32:42.237 回答
2

您必须“contentType”数据。

 var d = { 'filter': "John Portella" };
 $.ajax({
    type :  "POST",
    dataType: 'json',
    data: JSON.stringify(d),
    contentType: "application/json; charset=utf-8",
    url  :  "@routes.Search.findPag()",
    success: function(data){
        console.log(data);
    }
 });
于 2015-04-07T02:08:28.533 回答