我是 JSP 的新手。我的问题是,我想在按钮单击事件上使用 AJAX 更改我的 JSP 页面内容.. 我该怎么做..?
这是我的“AjaxTest”JSP 文件
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script type="text/javascript">
$(document).ready(function () {
$('#GetData').click(function () {
<%--
I want correct codes here,
that connects to my 'AjaxData' servlet
and get it's xml content by tags
and place them in '#PlaceData' paragraph.
--%>
});
</script>
</head>
<body>
<button id="GetData" onclick="loadData">Load</button>
<p id="PlaceData"></p>
</body>
</html>
这是我的“AjaxData”servlet GET 方法
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/xml");
response.setCharacterEncoding("UTF-8");
String content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ "<person1>"
+ "<firstname>"
+ "Indunil"
+ "</firstname>"
+ "<lastname>"
+ "Girihagama"
+ "</lastname>"
+ "</person1>";
response.getWriter().write(content);
}
请告诉我使用 jQuery AJAX 解决我的问题的正确代码。