Chr*_*ton 3 javascript ajax grails prototypejs
我正在尝试将JSON格式的数据从Javascript(使用Prototype)发布到Grails.我的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);
}
});
Run Code Online (Sandbox Code Playgroud)
我的Grails控制器中的代码是:
def receiveJson = {
def json = request.JSON;
}
Run Code Online (Sandbox Code Playgroud)
但是,在我的测试中,'json'变量似乎是空的.如果有人能解释我做错了什么,我会非常感激.非常感谢.
在您的Ajax.Request选项中更改
parameters:JSONstring,
Run Code Online (Sandbox Code Playgroud)
至
postBody:JSONstring,
Run Code Online (Sandbox Code Playgroud)
使用参数的问题是它对数据进行URL编码,以便请求体最终看起来像这样:
%7B%22id%22%3A%2223%22%2C%22name%22%3A%22Test%201%22%7D&_=
Run Code Online (Sandbox Code Playgroud)
而不是所需的(这是你用postBody得到的):
{"id":"23","name":"Test 1"}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10747 次 |
| 最近记录: |