我创建了一个名为roleManagement.jsp的jsp文件:
<%@page import="com.sun.org.apache.xalan.internal.xsltc.compiler.sym"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Role Management</title>
<script type="text/javascript" src="js/jquery-2.0.3.js"></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function() {
alert("entered in trial button code");
$.ajax({
type: "GET",
url:"/jquery2xdemo/RoleManagementContoller",
dataType: "json",
success: function (data) {
$.each(data.aaData,function(i, obj) {
alert(obj.value + ":" + obj.text);
var div_data = "<option value=" + obj.value + ">" + obj.text + "</option>";
alert(div_data);
$(div_data).appendTo('#ch_user1');
});
}
});
});
</script>
<div id="div_source1">
<select id="ch_user1" >
<option value="select"></option>
</select>
</div>
<input type="button" id="id_trial" name="btn_trial" value="Trial Button..">
</body>
</html>
在上面的代码中,我试图在$(document).ready
函数上调用我的servlet。但我无法做到这一点。我已将web.xml中的servlet信息映射为:
<servlet>
<description></description>
<display-name>RoleManagementContoller</display-name>
<servlet-name>RoleManagementContoller</servlet-name>
<servlet-class>com.programmingfree.controller.RoleManagementContoller</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RoleManagementContoller</servlet-name>
<url-pattern>/RoleManagementContoller</url-pattern>
</servlet-mapping>
但是当我运行我的jsp页面时,它根本没有进入document.ready函数。我究竟做错了什么。请帮忙。提前致谢
答案 0 :(得分:0)
在将url模式配置为/ RoleManagementContoller
时,您需要使用ajax调用url作为RoleManagementContoller而不是/ jquery2xdemo / RoleManagementContoller答案 1 :(得分:0)
检查jQuery是否正确连接到页面。 以下建议可能很有用:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.js"></script>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<script src="<c:url value="/resources/js/jQuery/jquery.js" />" ></script>