javascript - Send Post Json with ajax and Play Framework 2 - Stack Overflow

I have a problem regarding sending a json data to Play Controller.seach.scala.html$.ajax({type :"

I have a problem regarding sending a json data to Play Controller.

seach.scala.html

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

Controller : POST /find/findPag Search.findPag()

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

Debugging I get json = null . Which you think may be the problem?. Thank.

I have a problem regarding sending a json data to Play Controller.

seach.scala.html

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

Controller : POST /find/findPag Search.findPag()

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

Debugging I get json = null . Which you think may be the problem?. Thank.

Share Improve this question asked May 6, 2013 at 23:27 JohnPortellaJohnPortella 1,8215 gold badges22 silver badges30 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

You'll have to stringify the data. As it is right now I think that .toString() will be called on the data object and that is not something that can be correctly parsed as JSON on the server side.

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

You'll have to "contentType" the data.

 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);
    }
 });

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743744448a4499749.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信