我只是在GSP中尝试ajax Jquery函数,这里是GSP:
<%@ page contentType="text/html;charset=UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="layout" content="main" />
<title>Insert title here</title>
<g:javascript library='jquery' plugin='jquery' />
<script type="text/javascript">
function callAjax(){
$(document).ready(function(){
$('button').click(function(){
var URL="${createLink(controller:'book',action:'checkJquery')}"
$.ajax({
url:URL,
data: {id:'1'},
success: function(resp){
console.log(resp);
$("#author").val(resp.author)
$("#book").val(resp.bookName)
}
});
});
});
}
</script>
</head>
<body>
<button class="testMe" onclick="callAjax();">test</button>
<div class="body" id="divBody">
<g:textField name="author" id="author"/>
<g:textField name="book" id="book"/>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这是控制器中的checkJquery操作:
def checkJquery() {
def s=Book.get(params.id)
render s as JSON
}
Run Code Online (Sandbox Code Playgroud)
当我点击按钮时它没有做任何事情的问题,但是如果我再次点击它会在Chrome控制台中打印下面的问题,为什么从第一次点击它不起作用,以及为什么打印响应两次.
Object {class: "test.Book", …Run Code Online (Sandbox Code Playgroud)