-1

无法理解为什么ajax在调用我的 Play 控制器时这篇文章不起作用

 $.ajax({
        type: 'POST',
        url : '/login',
        dataType : 'json',
        data: "{username:"+user+",password:"+pass+"}",
        success : responseLogin,
        error : errorLogin
     });

我的 route.conf

 POST    /login                           controllers.UserController.authenticate()

控制器代码:

   DynamicForm data = form().bindFromRequest();
   User user =     User.authenticate( data.get("username"),data.get("password"));

但是这段代码有效:

  $.post('/login',
                    {'username':$("#txtUsername").val(),'password':$("#txtPassword").val()},
                    function(data) {
                      alert(data);
                    });
      });
4

1 回答 1

1

Why not change your $.ajax call to send data like your $.post call does? ($.post calls $.ajax under the hood.)

$.ajax({
        type: 'POST',
        url : '/login',
        dataType : 'json',
        data: {'username':$("#txtUsername").val(),'password':$("#txtPassword").val()},
        success : responseLogin,
        error : errorLogin
     });

Hope this helps.

于 2012-08-06T13:57:46.917 回答