最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cookies not in header when file requested by import - Stack Overflow

programmeradmin3浏览0评论

When I request my file like in the given code example (ES6 import) either using chromium or firefox I get the corresponding request on the server-side but as opposed to the "normal" script load (by script tag with src) cookies are not provided in the header, is that normal? And if so: How am I supposed to relate this request to a session if I have cookie based authentication?

Does not work - Because Browser does not send established cookie with the request:

<script type="module">
  import '/index.js';
</script>

Works - Browser sends cookie with the request:

<script src="/index.js">
</script>

When I request my file like in the given code example (ES6 import) either using chromium or firefox I get the corresponding request on the server-side but as opposed to the "normal" script load (by script tag with src) cookies are not provided in the header, is that normal? And if so: How am I supposed to relate this request to a session if I have cookie based authentication?

Does not work - Because Browser does not send established cookie with the request:

<script type="module">
  import '/index.js';
</script>

Works - Browser sends cookie with the request:

<script src="/index.js">
</script>
Share Improve this question edited Jun 28, 2017 at 13:18 jiron asked Jun 24, 2017 at 13:49 jironjiron 3512 silver badges12 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 12

I figured it out myself:

You have to add the "crossorigin" attribute to the script tag:

<script type="module" crossorigin>
  import '/index.js';
</script>

This is because es6 module import somehow goes under CORS even if the request for the module script goes to same-origin. And CORS does not send credentials for module script requests. ...Unless you put the crossorigin attribute.

This article of Jack Archibald helped me a lot (The browser issues mentioned naturally aren't quite up to date anymore):

https://jakearchibald./2017/es-modules-in-browsers/

You can put the cookies on the window to make it visible, otherwise the browser only sends headers with requests. Something like...

const window.cookie = document.cookie;

发布评论

评论列表(0)

  1. 暂无评论