文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Ajax基础教程学习(5)_读取响应首部

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>

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载