Ajax基础教程学习(5)_读取响应首部
时间:2010-08-31 来源:快乐的Tina
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Reading Response Headers</title>
<script type="text/javascript">
var xmlHttp;
var requestType = "";
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}
function doHeadRequest(request, url) {
requestType = request;
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("HEAD", url, true);
xmlHttp.send(null);
}
function handleStateChange() {
if(xmlHttp.readyState == 4) {
if(requestType == "allResponseHeaders") {
getAllResponseHeaders(); }
else if(requestType == "lastModified") {
getLastModified();
}else if(requestType == "isResourceAvailable") {
getIsResourceAvailable();
}
}
}
function getAllResponseHeaders() {
alert(xmlHttp.getAllResponseHeaders());
}
function getLastModified() {
alert("Last Modified: " + xmlHttp.getResponseHeader("Last Modified:"));
}
function getIsResourceAvailable() {
if(xmlHttp.status == 200) {
alert("Successful response"); }
else if(xmlHttp.status == 404) {
alert("Resource is unavailable"); }
else {
alert("Unexpected response status: " + xmlHttp.status);
}
}
</script>
</head>
<body> <h1>Reading Response Headers</h1>
<a href="javascript:doHeadRequest('allResponseHeaders','readingResponseHeaders.xml');">Read All Response Headers</a> <br/>
<a href="javascript:doHeadRequest('lastModified','readingResponseHeaders.xml');">Get Last Modified Date</a> <br/>
<a href="javascript:doHeadRequest('isResourceAvailable','readingResponseHeaders.xml');">Read Available Resource</a> <br/>
<a href="javascript:doHeadRequest('isResourceAvailable','not-available.xml');">Read Unavailable Resource</a>
</body>
</html>
readingResponseHeaders.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<readingResponseHeaders>
</readingResponseHeaders>