如何从jquery / javascript中的响应头读取数据

问题描述:


可能重复:

jQuery和AJAX响应头

如果返回服务器响应头中的数据我怎么读它。我正在向服务器发送一个AJAX请求。除响应标头中的位置外,它不会返回任何内容。我想使用JavaScript或jQuery读取该位置....

If the server is returned data in response header how I can read it. I am sending an AJAX request to a server. It does not return anything but the Location in response header. I want to read that location using JavaScript or jQuery....

两者 XMLHttpRequest jqXHR (这是jQuery包装AJAX请求的对象)有一个 getResponseHeader()方法,所以在 always() handler(jQuery)或readyState handler( XMLHttpRequest ),执行 this.getResponseHeader('Location')

Both XMLHttpRequest and jqXHR (which is the object jQuery wraps around AJAX requests) have a getResponseHeader() method, so in the always() handler (jQuery) or readyState handler (XMLHttpRequest), do this.getResponseHeader('Location').

请记住,如果您的服务器发送重定向状态代码(301/302),则重定向将 自动跟随,然后是无法访问返回的中间标头。

Bear in mind that if your server also sends a redirect status code (301/ 302) that redirect will be automatically followed, and there's no way to access the intermediate-headers returned.