简体   繁体   English

如何使用控制器动作中的post方法将grails模型呈现为javascript?

[英]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.

相关问题 如何使用remoteFunction将JavaScript变量传递给Grails控制器 - How can I pass javascript variable to grails controller using remoteFunction 如何使用 javascript 从视图中将 model 数据发布到 controller - How to post model data to controller from view using javascript Grails-如何将变量从控制器的操作返回到JavaScript? - Grails - how to return a variable from controller's action to JavaScript? 从控制器到gsp的Grails JavaScript渲染 - Grails javascript render from controller to gsp 如何使用ROR从控制器操作中调用javascript函数 - How can I call javascript functions from controller action using ROR 如何使用 javascript fetch 将参数传递给 action 方法? - How can i pass a parameters to action method using javascript fetch? 如何将简单的 Javascript 数组绑定到 MVC3 controller 操作方法? - How can I bind a simple Javascript array to an MVC3 controller action method? 当我想使用方法post(javascript)重定向到操作时,控制器方法不重定向并返回上一页,为什么? - When i want redirect to action with method post(javascript), controller method does not redirect and return to previous page, why? 如何从javascript文件中命中控制器操作方法 - how to hit controller action method from within javascript file 如何将JavaScript数据发布到Rails控制器动作? - How to post javascript data to a rails controller action?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM