Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 从grails中的gsp页面进行ajax调用_Jquery_Ajax_Grails - Fatal编程技术网

Jquery 从grails中的gsp页面进行ajax调用

Jquery 从grails中的gsp页面进行ajax调用,jquery,ajax,grails,Jquery,Ajax,Grails,我不熟悉ajax。我正试图从我的gsp页面向控制器操作发送请求。但我失败了。它没有调用控制器操作,页面正在重新加载。谁能看一下这个并帮帮忙吗。下面是我的查看页面>>> <%@ page contentType="text/html;charset=UTF-8" %> <html> <head> <title>Ajax First Example</title> <g:javascript plugin="jqu

我不熟悉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>
您可以尝试以下方法:

onclick="callAjax() return false;">
或者这个:

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

function callAjax(e){//谢谢您的帮助。第一个函数对我有用。但它不会对成功发出警报。只调用控制器的操作。我现在该怎么办?您可以尝试向ajax中添加错误函数,看看您的控制器是否正在生成
“json”
正确。是的,我的json构建不正确。现在我已经将其更改为html。现在可以了。但是请您给我一个完整ajax调用结构的链接。谢谢。
function callAjax(e){ //<-------pass the event
        e.preventDefault(); // <-----add this to prevent the default behavior
        $.ajax({
           .....
        });
}
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
            }
        });
    }