[英]How can I render a grails model to javascript using a post method from a controller action?
At a jquery event I want to call upon a controller action and use its data. 在jquery事件中,我想调用控制器操作并使用其数据。 The way I did it until now was: 到目前为止,我的方法是:
$('#my-button').click(function(){
$.post("${createLink(controller: 'myview', action: 'myAction')}", {param1: x, param2: y},
function(data){
//in here I want to use both elements of the model as I see fit.
})
});
My controller method looks like this: 我的控制器方法如下所示:
def myAction(param1, param2){
//some data manipulation goes here
render model: [returnValue1: variable1, returnValue2: variable2];
}
My question is how can I access variable1 and variable2 from my $.post method? 我的问题是如何从$ .post方法访问variable1和variable2? If I don't use a model and just return one of those variables, data will equal exactly that variable, but in this case it won't. 如果我不使用模型而仅返回这些变量之一,则数据将完全等于该变量,但在这种情况下则不会。
Can anybody tell me how to access those variables in that model please? 有人可以告诉我如何在该模型中访问那些变量吗? Just calling ${returnValue1} does not work in that function. 仅调用$ {returnValue1}在该函数中不起作用。
You can render a json object representing your model. 您可以渲染代表模型的json对象。 Here's the render documentation 这是渲染文档
render(contentType: "text/json") {
[returnValue1: variable1, returnValue2: variable2]
}
or 要么
render([returnValue1: variable1, returnValue2: variable2] as JSON)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.