javascript ajax 執行個體__ajax

來源:互聯網
上載者:User

原生態的簡單 的 javascript Ajax  只為做記錄:


//******************************** 頁面代碼

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>省市</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <script type="text/javascript"> var xmlHttp; // 建立xmlHttp對象 function createXMLHttpRequest() { if(window.ActiveXObject){ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } else if(window.XMLHttpRequest){ xmlHttp=new XMLHttpRequest(); } } function innitProvince() { //alert("ok"); createXMLHttpRequest(); var url = '<%=path%>/province.do?&method=innitProvince&date='+new Date().getTime(); //alert(xmlHttp.getAllResponseHeaders()); xmlHttp.open("POST",url,true); xmlHttp.onreadystatechange = callBack; xmlHttp.send(null); } function callBack() { if(xmlHttp.readyState==4) { if(xmlHttp.status==200) { createProvince(); } } } function createProvince() { var repXml = ""; repXml = xmlHttp.responseXML; //alert(xmlHttp.getAllResponseHeaders()); var names = repXml.getElementsByTagName("name"); var pro = document.getElementById("pro"); pro.lang = 0; for(var i=0;i<names.length;i++) { var name = names.firstChild.data; //alert(name); var option = document.createElement("<option>"); option.setAttribute("value",name); option.innerText = name; pro.appendChild(option); } } </script> </head> <body onload="innitProvince();"> <form action=""> <h1>省份</h1> <select id="pro" style="width: 150px;" > <option value="-1">請選擇</option> </select> </form> </body> </html> 

後台
public class ProvinceAction extends Action { /*   * Generated Methods   */ /**   * Method execute   * @param mapping   * @param form   * @param request   * @param response   * @return ActionForward   */ public ActionForward execute(ActionMapping mapping, ActionForm form,    HttpServletRequest request, HttpServletResponse response) { //  ProvinceForm provinceForm = (ProvinceForm) form;      System.out.println("---------------province is requested!------------------");      String method = request.getParameter("method");      String pr[] = {"北京","天津","上海","廣州","河北","山東"};   String city[] = {"1","2","3","4","5","6"};   StringBuffer sb = new StringBuffer();   if(method!=null&&method.trim().length()>0) {    try {          response.setContentType("text/xml;charset=UTF-8"); // 此句和下面的一定不能顛倒位置,否則頁面上會出現亂碼     PrintWriter pw = response.getWriter();     sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");          if(method.equalsIgnoreCase("innitProvince")) {      sb.append("<province>");      for(Object obj:pr) {       String pro = (String) obj;       sb.append("<name>"+pro+"</name>");      }      sb.append("</province>");      pw.write(sb.toString());      pw.flush();     }     else {      sb.append("<province>");      for(Object obj:city) {       String pro = (String) obj;       sb.append("<name>"+pro+"</name>");      }      sb.append("</province>");      pw.write(sb.toString());      pw.flush();     }         } catch (IOException e) {     e.printStackTrace();    }   }   return null; } } 




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.