我必须进行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/