Groovy / grails如何使用jquery从视图调用控制器方法 [英] Groovy/grails how to call controller method from view using jquery
问题描述
我有一个Groovy / Grails项目,其视图中有一些数据从其控制器呈现给它,比如控制器称为MyController,我想渲染的数据称为$ {myData}。我的观点的相关部分如下所示:
< head>
$('#refreshData')。click(function(){
//这里我想在我的控制器中运行该方法并更新myData
的值)
...
< / head>
< body>
...
< input type =textid =inputData/>
< button id =refreshData>提交< / button>
...
< / body>
我的控制器有一个动作接收一个String类型的参数(它应该从输入接收它)
def updateData(String input){
//更新输入
[myData:return_value]
$ b $ p
$ b我想从我的jquery函数中调用动作updateData, js变量赋值为该函数的返回值,或者通过将变量呈现给我的视图并访问它。我尝试调用
var newData = $ {
remoteFunction(
controller:'my',
action:'updateData',
params = [string_from_form]
)};
但是我收到错误 groovy.lang.MissingMethodException $ c
方法<>的签名不适用于值(java.util.LinkedHashMap,java .util.ArrayList)values:[[controller:my ...
有人可以告诉我如何调用控制器方法我的参数来自那个jQuery函数?
解决方案看起来您可能在代码的params部分有一个错误。尝试:
$ $ p $ $ $ $ $ $ $ $ $ $ $ $ $ $ ]')};
I have a Groovy/Grails project with a view that has some data rendered to it from its controller, say the controller is called MyController and the data I want to render is called ${myData}. The relevant part of my view looks like this:
<head>
$('#refreshData').click(function(){
//here I want to run the method in my controller and update the value of myData
});
...
</head>
<body>
...
<input type="text" id="inputData" />
<button id="refreshData">Submit</button>
...
</body>
My controller has an action that receives an argument of type String (it's supposed to receive it from the input form)
def updateData(String input) {
//updating input
[myData: return_value]
}
I want to call the action updateData from my jquery function either by having a local js variable assigned with the return value of that function or by having a variable rendered to my view and accessing it. I tried calling
var newData = ${
remoteFunction(
controller: 'my',
action: 'updateData',
params =[string_from_form]
)};
but I am getting the error groovy.lang.MissingMethodException
with a message of the form
No signature of method <> is applicable for values (java.util.LinkedHashMap, java.util.ArrayList) values: [[controller:my...
Can somebody please tell me how to call the controller method with my parameter from that jQuery function?
Looks like you might have a bug in the params section of your code. Try:
var newData = ${remoteFunction(controller: 'my', action: 'updateData', params: '[string_from_form]')};
这篇关于Groovy / grails如何使用jquery从视图调用控制器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!