jQuery   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jquery – 在grails中从gsp页面进行ajax调用大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我是ajax的新手.我正在尝试从我的gsp页面向控制器操作发送请求.但我失败了.它没有调用控制器操作,页面正在重新加载.任何人都可以看看这个和帮助.这是我的查看页面贝娄>>>

<%@ 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>

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

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

解决方法

你可以试试这个:

onclick="callAjax() return false;">

或者这个:

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

您要求的完整ajax呼叫:

function callAjax(){
        $.ajax({
            url: "returnMe",//          data:{ids:JSON.stringify(idList),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
            }
        });
    }

大佬总结

以上是大佬教程为你收集整理的jquery – 在grails中从gsp页面进行ajax调用全部内容,希望文章能够帮你解决jquery – 在grails中从gsp页面进行ajax调用所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: