I send data in JSON to the server like this:
var username = $("#usernameField").val();
var email = $("#emailField").val();
var password = $("#passwordField").val();
var parameters = {
'username':username,
'email':email,
'password':password
};
$.ajax({
type: "POST",
dataType:"JSON",
url: doRegistrationUrl,
data: parameters,
success: function(answer) {
console.log(answer);
}
});
this is the function who gets the data (a grails controller)
def doRegistration() {
def userdata = request.JSON;
printf("DataInController: "+userdata);
render userdata.username;
}
Url
var doRegistrationUrl = '${createLink(action:"doRegistration", controller:"registration")}';
however, the userdata is always null, the data doesnt get to the server. I cant see why. Anybody can help with this? Thanks a lot.
Your data parameters are just ordinary HTTP request parameters sent via POST. You deal with it just like every other controller action. Try: