Server端的代码如下:
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String name = req.getParameter("name");
String address = req.getParameter("address");
StringBuffer sb = new StringBuffer();
sb.append("<div><span>hi! nice to meet you! ");
sb.append(name).append(",(");
sb.append(address).append("),");
sb.append("how are you getting along?</span></div>");
StringBuffer xmlSb = new StringBuffer();
xmlSb.append("<book>")
.append("<title>")
.append("No ventured No gained")
.append("</title>")
.append("<publish>")
.append("Chinses publish")
.append("</publish>")
.append("</book>");
//resp.setContentType("text/html;charset=utf-8");
resp.setContentType("text/xml;charset=utf-8");
resp.getWriter().print(xmlSb.toString());
}
前端的代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript" src="jquery-1.8.3.js"></script>
<script type="text/javascript">
$(function(){
$("#getContentByAjax").click(function(){
//$.post("test",{name:"czz",address:"address"},callBack,"html");
//$.get("test",{name:"czz",address:"address"},callBack,"html");
$.get("test",{name:"czz",address:"address"},callBack,"xml");
});
function callBack(data,textStatus){
//$("#showText").html(data);
var title=$(data).find("title").text();
var publish = $(data).find("publish").text();
$("#showText").text(title+" "+publish);
}
});
</script>
</head>
<body>
<div id="showText"></div>
<input type="button" id="getContentByAjax" value="sendAjax">
</body>
</html>
jQuery_review之通过$.get()和$.post()方法来实现异步加载,布布扣,bubuko.com
jQuery_review之通过$.get()和$.post()方法来实现异步加载
原文:http://blog.csdn.net/ziwen00/article/details/38177925