I have a controller that takes some json in the ?request body? and does awesome things with it:
def myController(){
def myAction(){
println "Here is request.JSON: ${request.JSON as JSON}"
println "Here is params: $params"
//do awesome stuff with request.JSON only
return
}
}
So I can hit this with cURL like so:
curl -i -H "content-type: application/json" -d "{\"someVariable\":\"Absolutely\"}"
And my grails controller prints:
Here is request.JSON: {"someVariable":"Absolutely"}
Here is params: [controller:'myController', action:'myAction']
So far so good, but when I try to do this with jQuery it goes in params!!!
Having read these two questions: Setting the POST-body to a JSON object with jQuery
jQuery posting valid json in request body
My best guess for how to write the .js is:
var sendMe = {"someVariable":"Absolutely"}
$.ajax({
url: '/myController/myAction',
type: 'POST',
processData: false,
data: JSON.stringify(sendMe),
dataType: 'json',
success: function(data) {
},
error: function(request, status, error) {
}
});
But when I do this my controller prints:
Here is request.JSON: {}
Here is params: [{"someVariable":"Absolutely"}:, controller:'myController', action:'myAction']
I must be doing something wrong with jQuery.
UPDATE: looks like this idiot was actually having the same problem: How to get at JSON in grails 2.0 but instead of confronting the jQuery issue he just used cURL and the reqeust.JSON thing. What a lazy guy.