简体   繁体   English

AJAX将JSON数据从Javascript发布到Grails

[英]AJAX post JSON data from Javascript to Grails

I'm trying to POST JSON formatted data from Javascript (using Prototype) to Grails. 我正在尝试将JSON格式的数据从Javascript(使用Prototype)发布到Grails。 My Javascript code is: 我的Javascript代码是:

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: 我的Grails控制器中的代码是:

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

However, the 'json' variable appears to be empty in my tests. 但是,在我的测试中,'json'变量似乎是空的。 I'd be so grateful if someone could explain what I'm doing wrong. 如果有人能解释我做错了什么,我会非常感激。 Many thanks. 非常感谢。

In your Ajax.Request options change 在您的Ajax.Request选项中更改

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: 使用参数的问题是它对数据进行URL编码,以便请求体最终看起来像这样:

%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): 而不是所需的(这是你用postBody得到的):

{"id":"23","name":"Test 1"}

Good question mfloryan - I was doing the testing manually, ie not as part of a unit or integration test. 好问题mfloryan - 我正在手动进行测试,即不作为单元或集成测试的一部分。

Thanks very much for the help hvgotcodes . 非常感谢帮助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 . 有趣的是,如果我打印request.JSON我得到{} ,而如果我打印request.json我得到null

EDIT: By 'printing' I mean using: request.JSON.toString() 编辑:通过'打印'我的意思是使用: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. 一旦我做出最后的改变,John Wagenleitne建议代码开始正常工作。 I'm very grateful indeed for all your help. 我非常感谢你的帮助。

I don't think you are invoking the Ajax.Request correctly. 我认为你没有正确地调用Ajax.Request。 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." “请求的参数,将被编码到'get'方法的URL中,或者编码到其他方法的请求主体中。这可以作为URL编码的字符串或任何与Hash兼容的对象提供(基本上是任何东西),具有代表参数的属性。

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. 注意参数对象文字的形式 - 它告诉Prototype库将请求键'json'和请求值作为json字符串。 You access the key off the request object in the controller. 您可以从控制器中的请求对象访问该密钥。

EDIT -- I just realized you're javascript block is jacked up. 编辑 - 我刚刚意识到你是javascript块被搞砸了。

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 = {};
....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM