js如何获取response header信息

JavaScript051

js如何获取response header信息,第1张

$.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')

}

// 在这里,做想做的事。。。

}

})

1.先实现一个对Response的包装器:

Java代码

public class StatusExposingServletResponse extends HttpServletResponseWrapper {

private int httpStatus

public StatusExposingServletResponse(HttpServletResponse response) {

super(response)

}

@Override

public void sendError(int sc) throws IOException {

httpStatus = sc

super.sendError(sc)

}

@Override

public void sendError(int sc, String msg) throws IOException {

httpStatus = sc

super.sendError(sc, msg)

}

@Override

public void setStatus(int sc) {

httpStatus = sc

super.setStatus(sc)

}

public int getStatus() {

return httpStatus

}

}

2.然后实现一个Filter来替换原始的HttpServletResponse,这样你就可以在Filter里面取到statusCode了

Java代码

public class StatusReportingFilter implements Filter {

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {

StatusExposingServletResponse response = new StatusExposingServletResponse((HttpServletResponse)res)

chain.doFilter(req, response)

int status = response.getStatus()

// report

}

public void init(FilterConfig config) throws ServletException {

//empty

}

public void destroy() {

// empty

}

}

// ajax 对象

function ajaxObject() {

    var xmlHttp

    try {

        // Firefox, Opera 8.0+, Safari

        xmlHttp = new XMLHttpRequest()

        } 

    catch (e) {

        // Internet Explorer

        try {

             xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")

            } catch (e) {

            try {

                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")

            } catch (e) {

             alert("您的浏览器不支持AJAX!")

             return false

            }

        }

}

    return xmlHttp

}

// ajax post请求:

function ajaxPost ( url , data , fnSucceed , fnFail , fnLoading ) {

    var ajax = ajaxObject()

    ajax.open( "post" , url , true )

    ajax.setRequestHeader( "Content-Type" , "application/x-www-form-urlencoded" )

    ajax.onreadystatechange = function () {

        if( ajax.readyState == 4 ) {

            if( ajax.status == 200 ) {

                fnSucceed( ajax.responseText )

            }

            else {

                fnFail( "HTTP请求错误!错误码:"+ajax.status )

            }

        }

        else {

            fnLoading()

        }

    }

    ajax.send( data )

}