Unable to read custom http headers in javascript onreadystatechange?

徘徊边缘 提交于 2019-12-01 10:41:39

If someone still looking for the answer, you need to set "Access-Control-Expose-Headers" along side with the response. In example :

Access-Control-Expose-Headers: Content-Type,Authorization,X-RateLimit-Limit,X-RateLimit-Remaining,X-RateLimit-Reset

change your callback:

xhr.onreadystatechange = function() {
      if(xhr.readyState == 4){//if you are looking only for HTTP 200, then add condition xhr.status == 200
         console.log(xhr.getResponseHeader("Authorization"));//will get your response header
         window.location.href = xhr.responseURL;//Redirect works
      }
  }

Note: readyState Holds the status of the XMLHttpRequest. Changes from 0 to 4:

0: request not initialized

1: server connection established

2: request received

3: processing request

4: request finished and response is ready

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!