3

I'm trying to POST JSON formatted data from Javascript (using Prototype) to Grails. My Javascript code is:

var JSONObject = new Object;
    JSONObject.id = "23";
    JSONObject.name = "Test 1";
    JSONstring = JSON.stringify(JSONObject);



 var url = "${createLink(controller:'testController', action:'receiveJson')}";
    new Ajax.Request(url, {
      method:'post',
      contentType:'application/json',
      parameters:JSONstring,
      asynchronous:true,
      onSuccess: function (req) {
        sendInfoResponse(req.responseText);
      }
    });

and the code in my Grails controller is:

def receiveJson = {
  def json = request.JSON;
}

However, the 'json' variable appears to be empty in my tests. I'd be so grateful if someone could explain what I'm doing wrong. Many thanks.

1
  • 1
    Do you mean manual tests of the application or proper unit / integration tests? Commented Jan 1, 2011 at 21:48

3 Answers 3

5

In your Ajax.Request options change

parameters:JSONstring,

to

postBody:JSONstring,

The problem with using parameters is that it URL encodes the data so that the request body ends up looking like this:

%7B%22id%22%3A%2223%22%2C%22name%22%3A%22Test%201%22%7D&_=

Instead of the desired (which is what you get with postBody):

{"id":"23","name":"Test 1"}
Sign up to request clarification or add additional context in comments.

2 Comments

Or you could do parameters:JSONObject, and leave out JSONstring entirely, Prototype will encode it correctly for you.
@clockworkgeek that will encode the object as key/value pairs: id=23&name=Test%201&_=. You could access this as params.id and params.name in the controller, but it wont be parsed as a JSON object via request.JSON which is what I think the original poster wanted.
1

Good question mfloryan - I was doing the testing manually, i.e. not as part of a unit or integration test.

Thanks very much for the help hvgotcodes. I made the changes to my code as you have suggested, but unfortunately to no avail. Interestingly, if I print request.JSON I get {}, whereas if I print request.json I get null.

EDIT: By 'printing' I mean using: request.JSON.toString()

EDIT: Thank you all so much for the help. Once I'd made the final change John Wagenleitne suggested the code began working properly. I'm very grateful indeed for all your help.

1 Comment

If the answer you referenced solved the problem, then you should mark the answer as accecpted.
0

I don't think you are invoking the Ajax.Request correctly. From the documentation, the parameters option:

"The parameters for the request, which will be encoded into the URL for a 'get' method, or into the request body for the other methods. This can be provided either as a URL-encoded string or as any Hash-compatible object (basically anything), with properties representing parameters."

I think you need to do something like

...
parameters: {json: JSONString}
...

and then in your controller

request.json

note the form of the parameters object literal - it tells the Prototype library to make the request key 'json' and the request value be the json string. You access the key off the request object in the controller.

EDIT -- I just realized you're javascript block is jacked up.

This:

var JSONObject = new Object;

should be something like

var JSONObject = new Object();
...

you might also be able to do just an object literal, so

var jsonObject = {};
....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.