请从Grails的GSP页面Ajax调用 [英] Make ajax call from gsp page in grails

查看:416
本文介绍了请从Grails的GSP页面Ajax调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的AJAX。我想从我的GSP页面请求发送到控制器动作。但我失败了。他并没有叫控制器动作和页面重新加载。任何人都可以请看看这个和帮助。这是我的看法页面娄>>>

 <%@页面的contentType =text / html的;字符集= UTF-8%>
< HTML>
< HEAD>
  <冠军>阿贾克斯第一例< /标题>
    < G:JavaScript的插件=jQuery的库=jQuery的SRC =的jQuery / jQuery的-1.7.2.js/>
    <脚本>
        传播callAjax(){
            $阿贾克斯({
                网址:returnMe
                类型:后,
                数据类型:JSON,
//数据:{IDS:JSON.stringify(IDLIST),选项:选项,ID:ID}
                成功:函数(){
                    警报(1)
                }
            });
        }
    < / SCRIPT>
< /头>
<身体GT;
<表格名称=myForm的'>
    <输入类型=提交值=调用Ajax的功能的onclick =callAjax()>
< /形式GT;
< /身体GT;
< / HTML>
 

这是我的控制器操作>>>

 高清returnMe = {
    弦乐味精=sdfsdf
    味精呈现为JSON
}
 

解决方案

您可以试试这个:

 的onclick =callAjax()返回false;>
 

或者这一个:

 函数callAjax(五){//< -------传递事件
        即preventDefault(); //< -----将它添加到prevent的默认行为
        $阿贾克斯({
           .....
        });
}
 

您完整的Ajax调用的要求:

 函数callAjax(){
        $阿贾克斯({
            网址:returnMe
            类型:后,
            数据类型:JSON,
//数据:{IDS:JSON.stringify(IDLIST),选项:选项,ID:ID}
            成功:功能(数据){
                的console.log(数据); //< -----这会将数据在浏览器的控制台
            },
            错误:函数(XHR){
                警报(xhr.responseText); //< ----当没有数据提醒错误信息
            }
        });
    }
 

I am new to ajax. I am trying to send a request from my gsp page to controller action. But I am failing. It is not calling controller action and the page is reloading. Can anyone please look at this and help. Here is my view page bellow >>>

    <%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
  <title>Ajax First Example</title>
    <g:javascript plugin="jquery" library="jquery" src="jquery/jquery-1.7.2.js"/>
    <script>
        function callAjax(){
            $.ajax({
                url: "returnMe",
                type:"post",
                dataType: 'json',
//            data:{ids:JSON.stringify(idList), option:option, id:id}
                success: function() {
                    alert(1)
                }
            });
        }
    </script>
</head>
<body>
<form name='myForm'>
    <input type="submit" value="Call Ajax Function" onclick="callAjax()">
</form>
</body>
</html>

here is my controller action >>>

def returnMe = {
    String msg = 'sdfsdf'
    render msg as JSON
}

解决方案

You can try this :

onclick="callAjax() return false;">

or this one:

function callAjax(e){ //<-------pass the event
        e.preventDefault(); // <-----add this to prevent the default behavior
        $.ajax({
           .....
        });
}

Your complete ajax call as requested:

function callAjax(){
        $.ajax({
            url: "returnMe",
            type:"post",
            dataType: 'json',
//          data:{ids:JSON.stringify(idList), option:option, id:id}
            success: function(data) {
                console.log(data); //<-----this logs the data in browser's console
            },
            error: function(xhr){
                alert(xhr.responseText); //<----when no data alert the err msg
            }
        });
    }

这篇关于请从Grails的GSP页面Ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆