ajax - Grails:来自Ajax调用的数据未显示在Controller的params变量中

标签 ajax grails

我必须进行ajax调用以获取一些数据。奇怪的是,数据不会正确地传输到 Controller 中的操作:

这是我的ajax电话:

var params = [id: "3", version: "58", selected: "true", format: "json"]

 $.ajax({
            type: "POST",
            cache: false,
            url: "/selection/ajaxUpdate",
            dataType: 'json',
            data: params,
            success:function(data,textStatus){
                console.log("success: ",data)
            },
            error:function(XMLHttpRequest,textStatus,errorThrown){
                console.log("error: ",XMLHttpRequest)
            }
        });

这是我在 Controller “选择”中的 Action :
@Transactional    
def ajaxUpdate(){

    println params
    .... other stuff ...
}

println params命令仅输出:“[controller:selection,format:null,action:ajaxUpdate]”

因为println,所以显然调用了该 Action ,但是没有ajax params :(

有人知道解决方案吗?

提前Thx

最佳答案

您需要告诉Grails操作接受JSON参数

@Transactional    
def ajaxUpdate(){
    def jsonParams = request.JSON
    println jsonParams   // this will print the ajax params

    println params
    .... other stuff ...
}

关于ajax - Grails:来自Ajax调用的数据未显示在Controller的params变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43639366/

相关文章:

ajax - 在跨域请求中,JSONP 比 JSON 安全吗?

jquery - IE 中存在“JSON”未定义问题,但 chrome 中没有

javascript - 与Grails <g:each>合并重复的列表中的JSON数据

grails - 如何在 grails 3 中将 gson View 呈现为字符串?

grails - 呈现为JSON在Grails中不起作用

grails - grails使用URL映射文件中的状态响应错误页面

php - 如何使用 ajax 和 PHP 计算和显示实时数据库查询?

javascript - 访问 Prototype 中 onSuccess 函数内设置的值

php - 当变量变为 TRUE 时弹出警报

grails - 在没有 html 和 body 标签的情况下在 grails 中渲染页面