脚本请求未与身份验证标头一起发送

时间:2018-08-29 08:07:47

标签: html http basic-authentication es6-modules

我的页面上有基本身份验证。验证了我对页面的请求及其资源(CSS等)后,便有了Authentication标头

Authorization   Basic NOTREALNOTREALNOTREALNOTREALNOTREAL=

在正文末尾,我正在加载一个外部脚本。

<script type="module" src="/main.js"></script>

此请求未与身份验证标头一起发送。这是页面上唯一没有的东西。这会导致带有以下标头的401响应

WWW-Authenticate    Basic realm="mydomain.com"

在chrome和Firefox中都会发生这种情况。

有人知道为什么会这样吗?如何发送?

1 个答案:

答案 0 :(得分:2)

Looks like this is a quirk of <script type="module">,这会将它们全部视为CORS请求,因此默认情况下它们不发送Authentication标头。

可以通过在脚本标签中添加crossorigin="use-credentials"属性来解决此问题:

<script type="module" src="/main.js" crossorigin="use-credentials"></script>

应该工作。

相关问题