javascript - 如何从我的 Controller grails 调用 js 函数

标签 javascript jquery ajax grails

我是 grails 新手,如果有人能帮助我就好了。

我有一个 js 函数,在 Controller 中执行一些操作后,我会在 Controller 中调用该函数来打开弹出窗口。

我有这个 gsp 文件:

<g:form controller="admin" method="post">
<table summary="generer PDF">
..
..
<td>
<g:actionSubmit value="generate" action="genererAction"/>
</td>
</tr>
</table>
</g:form>

当用户单击“生成”(按钮)时,必须调用此函数:

def generateAction = {
def result
...
if (result){ 
//here, if result=true a popup will be displayed
}
else  {
redirect(action: 'index')
}}

我希望你能帮助我。谢谢。

最佳答案

你可以使用ajax来做到这一点,这是一个例子:

查看:

<g:form controller="admin" method="post">
<table id="tableID" summary="generer PDF">
..
..
<td>
<!--<g:actionSubmit value="generate" action="genererAction"/>-->
<button onclick="preformAction(); return false;">generate</button>
</td>
</tr>
</table>
</g:form>

Controller :

def generateAction = {
def result
...
if (result){ 
//here, if result=true a popup will be displayed
render "success"
}
else  {
//redirect(action: 'index')
render "index"
}}

Script ://将此脚本复制到您的 View 中

<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript">
function preformAction(){
    $.ajax({
        url:'${createLink(controller:"admin",action:"generateAction")}', // here you can pass the controller url
        data: {
            // data to be passed to controller
        },
        success : function(response){
            // this block is called after successful processing of controller
            // here you can call your js function after controller has finished its processing
            // consider response has 'success'
            if(response == 'success'){
                 OpenSAGPopup();
            } else if(response == 'index'){
                 window.location = '${createLink(controller:"admin",action:"index")}';
            }

        },
        error : function(response){
            // this block executes if any error occurs with the processing of controller
            alert("Error while generating pdf");
        },
    });
}
</script>

试试这个,如果有效请告诉我......

祝你编码愉快。

关于javascript - 如何从我的 Controller grails 调用 js 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31696822/

相关文章:

javascript - 链接表单以检索多个范围信息

javascript - jQuery 变量作用域/调用顺序

javascript - Jquery 工具提示不适用于新的 ajax 追加元素

ASP.NET AJAX 和 session 变量

javascript - 用 every 和 $.post 关闭

javascript - 数据表显示错误但数据显示不正确

javascript - 使用 Jquery 将 TD 标签放入对象中?

php - 无法加载所有 iframe

javascript - google.load - 和消息 "google is not defined"

javascript - 三.js加载json字符串