jQuery_review之通过$.get()和$.post()方法来实现异步加载
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>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。