jQuery+ajax+xml

來源:互聯網
上載者:User
 
 

2.前段頁面代碼

 <body id="theBody">  <select id="name">  <option value="zhangsan">zhangsan</option>  <option value="lisi">lisi</option>  </select>   <input type="button" id="button1" value="get content from server">  </body>


 

3.javaBean--Person

 

public class Person {private int id;private String name;private int age;private String address;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}}

4.伺服器端代碼--XMLServlet

protected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {String name = req.getParameter("name");Person person = new Person();if("zhangsan".equals(name)){person.setId(1);person.setName("zhangsan");person.setAddress("tianjin");person.setAge(23);}else{person.setId(2);person.setName("lisi");person.setAddress("beijing");person.setAge(30);}//產生XML--DOM4JDocument document = DocumentHelper.createDocument();Element rootElement = document.addElement("users");rootElement.addComment("This is a commont");Element userElement = rootElement.addElement("user");Element idElement = userElement.addElement("id");Element nameElement = userElement.addElement("name");Element ageElement = userElement.addElement("age");Element addressElement = userElement.addElement("address");idElement.setText(person.getId()+"");nameElement.setText(person.getName());ageElement.setText(person.getAge()+"");addressElement.setText(person.getAddress());resp.setContentType("text/html; charset=utf-8"); //設定回應標頭resp.setHeader("pragma", "no-cache"); //無緩衝resp.setHeader("cache-control", "no-cache"); //無緩衝PrintWriter out = resp.getWriter();OutputFormat format = OutputFormat.createPrettyPrint(); //為了方便在firebug裡面查format.setEncoding("utf-8");XMLWriter xmlWriter = new XMLWriter(out,format); //寫到用戶端xmlWriter.write(document);out.flush();}

5.前端頁面處理指令碼

$("#button1").click(function(){$.ajax({ //no need to judge the browser typetype: "POST", //類型url: "XMLServlet", //請求訪問的servletdataType: "xml",   data: {'name': $("#name").val()}, //傳遞的參數,擷取選擇的值success: function(returnedData){   //成功返回的回呼函數//解析傳來的XML--jQuery對象var id = $(returnedData).find("id").text(); //找到id元素並取其值var name = $(returnedData).find("name").text();var age = $(returnedData).find("age").text();var address = $(returnedData).find("address").text();//建立表格顯示格式var html="<table width='60%' border='1' align='center'><tr><th>id</th><th>name></th><th>age</th><th>address</th></tr><tr align='center'><td>"+id+"</td><td>"+name+"</td><td>"+age+"</td><td>"+address+"</td></tr></table>";$("#theBody table:eq(0)").remove(); //刪除先前的表格$("#theBody").append(html);}});});

 

6.前端頁面處理指令碼--簡易形式

$("#button1").click(function(){$.post("XMLServlet",{name:$("#name").val()},function(returnedData,status){//解析傳來的XML--jQuery對象var id = $(returnedData).find("id").text(); //找到id元素並取其值var name = $(returnedData).find("name").text();var age = $(returnedData).find("age").text();var address = $(returnedData).find("address").text();//建立表格顯示格式var html="<table width='60%' border='1' align='center'><tr><th>id</th><th>name></th><th>age</th><th>address</th></tr><tr align='center'><td>"+id+"</td><td>"+name+"</td><td>"+age+"</td><td>"+address+"</td></tr></table>";$("#theBody table:eq(0)").remove(); //刪除先前的表格$("#theBody").append(html);});});


 

$("#button1").click(function(){$.post("XMLServlet",{name:$("#name").val()},function(returnedData,status){//解析傳來的XML--jQuery對象var id = $(returnedData).find("id").text(); //找到id元素並取其值var name = $(returnedData).find("name").text();var age = $(returnedData).find("age").text();var address = $(returnedData).find("address").text();//建立表格顯示格式var html="<table width='60%' border='1' align='center'><tr><th>id</th><th>name></th><th>age</th><th>address</th></tr><tr align='center'><td>"+id+"</td><td>"+name+"</td><td>"+age+"</td><td>"+address+"</td></tr></table>";$("#theBody table:eq(0)").remove(); //刪除先前的表格$("#theBody").append(html);});});


 

$("#button1").click(function(){$.ajax({ //no need to judge the browser typetype: "POST", //類型url: "XMLServlet", //請求訪問的servletdataType: "xml",   data: {'name': $("#name").val()}, //傳遞的參數,擷取選擇的值success: function(returnedData){   //成功返回的回呼函數//解析傳來的XML--jQuery對象var id = $(returnedData).find("id").text(); //找到id元素並取其值var name = $(returnedData).find("name").text();var age = $(returnedData).find("age").text();var address = $(returnedData).find("address").text();//建立表格顯示格式var html="<table width='60%' border='1' align='center'><tr><th>id</th><th>name></th><th>age</th><th>address</th></tr><tr align='center'><td>"+id+"</td><td>"+name+"</td><td>"+age+"</td><td>"+address+"</td></tr></table>";$("#theBody table:eq(0)").remove(); //刪除先前的表格$("#theBody").append(html);}});});


 

 
 
 
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.