【发布时间】:2016-01-02 19:09:51
【问题描述】:
我只是在 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>
这是控制器中的 checkJquery 操作:
def checkJquery() {
def s=Book.get(params.id)
render s as JSON
}
当我单击按钮时,它什么也不做,但是如果我再次单击它,它会在 chrome 控制台中打印以下内容,问题为什么从第一次单击它不起作用,以及为什么打印响应两次。
Object {class: "test.Book", id: 1, author: "a1", bookName: "book1"}
Object {class: "test.Book", id: 1, author: "a1", bookName: "book1"}
【问题讨论】:
-
即使我评论了
$(document).ready()仍然是相同的行为
标签: javascript jquery ajax grails