`

readingResponseHeaders.html

阅读更多
例子来源:《Ajax 基础教程》 金灵 等译 这本书非常不错

readingResponseHeaders.html

<html>
<head>
<title>Reading Response Header</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="javascipt:doHeadRequest('isResourceAvailable', 'not-available.xml');">Read Unavailable Resource</a>
</body>
</html>


readingResponseHeaders.xml

<?xml version='1.0' encoding='UTF-8' ?>
<readingResponseHeaders>

</readingResponseHeaders>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics