廢話不多說,代碼如下:
Ajax.jsp代碼
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<html>
<head>
<script language="javascript">
var xmlHttp;
function createXMLHttp(){
if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}else{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
function getInfo(){
createXMLHttp();
xmlHttp.open("POST", "allarea.xml");
xmlHttp.onreadystatechange = getCityCallback ;
xmlHttp.send(null);
}
function getCityCallback(){
if(xmlHttp.readyState == 4 ){
alert("------OOO--");
if(xmlHttp.status == 200){
var allarea = xmlHttp.responseText;
alert(allarea);
}
}
}
</script>
</head>
<body onload = "getInfo()">
<form action="" method="post">
</form>
</body>
</html>
allarea.xml 檔案
<?xml version="1.0" encoding="UTF-8"?>
<allarea>
<area>
<id>1</id>
<title>北京</title>
</area>
<area>
<id>2</id>
<title>上海</title>
</area>
<area>
<id>3</id>
<title>深圳</title>
</area>
</allarea>