Javascript fetch api: unable to get response header

I am using the javascript fetch API to request a cross-domain api using this code:

fetch('https://api.com/search?query="2016"').then(function (response) {

  console.log(response.headers.get('Access-Control-Allow-Headers'))

  console.log(response.headers.get('Content-Range'))
  console.log(response.headers.get('Accept-Range'))

  console.log(response.headers.get('Date'))
  console.log(response.headers.get('Content-Type'))
})

The response headers are as follows:

Accept-Range:cars 300
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:Origin, X-Requested-With, Content-Type, Accept, Content-Range, Accept-Range
Access-Control-Allow-Methods:PUT, POST, OPTIONS, GET
Access-Control-Allow-Origin:*
Cache-Control:no-cache
Connection:keep-alive
Content-Range:0-99/1941
Content-Type:application/json
Date:Tue, 12 Jan 2016 12:17:55 GMT
Transfer-Encoding:chunked

It is very strange that only "Content-Type" works for others, I get null:

null
null
null
null
application/json

What do I need to do to get these null headers?

+4
source share
1 answer

Damn, I used "Access-Control-Allow-Headers" instead of "Access-Control-Expose-Headers"

Now it works

+5
source

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


All Articles