Accessing the web page's HTTP Headers in JavaScript

前端 未结 17 2678
日久生厌
日久生厌 2020-11-21 04:53

How do I access a page\'s HTTP response headers via JavaScript?

Related to this question, which was modified to ask about accessing two specific HTTP headers.

<
17条回答
  •  Happy的楠姐
    2020-11-21 05:46

    As has already been mentioned, if you control the server side then it should be possible to send the initial request headers back to the client in the initial response.

    In Express, for example, the following works:

    app.get('/somepage', (req, res) => { res.render('somepage.hbs', {headers: req.headers}); }) The headers are then available within the template, so could be hidden visually but included in the markup and read by clientside javascript.

提交回复
热议问题