JQuery AJAX custom response headers

I am using some API and I noticed that in the answer I have this:

Chrome console screen

I will need to read "x-dl-units-left", but I get null:

$.ajax(ajaxConfig).done(function(response, textStatus, xhr){
  var left = xhr.getResponseHeader("x-dl-units-left"); //null
  var all = xhr.getAllResponseHeaders(); // "content-type: application/json;charset=UTF-8"
});

Anyone who can know why ?: (

thank

+5
source share
2 answers

The problem is that the request uses CORS. Therefore, you need to explicitly allow access to your custom headers for the recipient. To do this, add a header Access-Control-Expose-Headersto the answer, for example:

Access-Control-Expose-Headers: x-dl-units-left, x-dl-units, [other headers as needed...]

, , . , . API.

+7

, , . . :

 Acccess-Control-Expose-Headers: x-dl-units-left;

 $.ajax(ajaxConfig).done(function(response, textStatus, xhr){

  var all = xhr.getAllResponseHeaders(); 
 // "content-type: application/json;charset=UTF-8"

});
0

Source: https://habr.com/ru/post/1685293/


All Articles