javascript ajax 实例

原生态的简单 的 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 οnlοad="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; 
} 
} 




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值