Ajax获取Response头信息

$.ajax({
    type: 'HEAD', // 获取头信息,type=HEAD即可
    url : window.location.href,
    complete: function( xhr,data ){
        // 获取相关Http Response header
        var wpoInfo = {
            // 服务器端时间
            "date" : xhr.getResponseHeader('Date'),
            // 如果开启了gzip,会返回这个东西
            "contentEncoding" : xhr.getResponseHeader('Content-Encoding'),
            // keep-alive ? close?
            "connection" : xhr.getResponseHeader('Connection'),
            // 响应长度
            "contentLength" : xhr.getResponseHeader('Content-Length'),
            // 服务器类型,apache?lighttpd?
            "server" : xhr.getResponseHeader('Server'),
            "vary" : xhr.getResponseHeader('Vary'),
            "transferEncoding" : xhr.getResponseHeader('Transfer-Encoding'),
            // text/html ? text/xml?
            "contentType" : xhr.getResponseHeader('Content-Type'),
            "cacheControl" : xhr.getResponseHeader('Cache-Control'),
            // 生命周期?
            "exprires" : xhr.getResponseHeader('Exprires'),
            "lastModified" : xhr.getResponseHeader('Last-Modified')
        };
        // 在这里,做想做的事。。。
    }
});


 

xhr.getAllResponseHeaders();//返回全部头信息,string
xhr.getResponseHeader('date');
xhr.getResponseHeader('server');
xhr.getResponseHeader('transfer-Encoding');
xhr.getResponseHeader('content-type')

  

 

function top10(){
	mAjax('http://v.acc5.com/mplay_3733.mp4?_token=24a6vcFglac4ePsMMFLLSCXQzmTkft21BVoFBLcVxOF7byOa',function(ret){
		console.log(ret);
	},function(err){
		console.log('errerr');
	},'','get')
}


function mAjax(url,success,fail,data,type){
	var xhr = new XMLHttpRequest();
	xhr.open(type, url, true);
	xhr.responseType = "text";
	xhr.onload = function() {
		if (this.status == 200) {
			var obj=xhr.getAllResponseHeaders();//xhr.responseText;
			success&&success(obj)
		}
	}
	xhr.send(data);
}
top10()

  

posted on 2019-03-07 18:31  神奇的旋风  阅读(7061)  评论(0编辑  收藏  举报

导航