【问题标题】:How can I render a grails model to javascript using a post method from a controller action?如何使用控制器操作中的 post 方法将 grails 模型渲染为 javascript?
【发布时间】:2013-08-07 18:27:31
【问题描述】:

在一个 jquery 事件中,我想调用一个控制器动作并使用它的数据。到目前为止,我的做法是:

$('#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.
})

});

我的控制器方法如下所示:

def myAction(param1, param2){
//some data manipulation goes here

render model: [returnValue1: variable1, returnValue2: variable2];
}

我的问题是如何从我的 $.post 方法访问 variable1 和 variable2?如果我不使用模型而只返回其中一个变量,数据将完全等于该变量,但在这种情况下不会。

谁能告诉我如何访问该模型中的这些变量?仅调用 ${returnValue1} 在该函数中不起作用。

【问题讨论】:

    标签: javascript jquery grails


    【解决方案1】:

    您可以渲染一个代表您的模型的 json 对象。这是render documentation

    render(contentType: "text/json") {
        [returnValue1: variable1, returnValue2: variable2]
    }
    

    render([returnValue1: variable1, returnValue2: variable2] as JSON)
    

    【讨论】:

      猜你喜欢
      • 2012-03-27
      • 1970-01-01
      • 2014-05-01
      • 2010-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多