我是阿贾克斯新手。我正在尝试从 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",
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
}
});
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)