错误:由于 MIME 类型(“text/html”)不匹配而被阻止(X-Content-Type-Options:nosniff)

mic*_*ith 6 html javascript jquery express

我正在尝试使用 express 和 ajax 在测试站点上测试一些 api 调用,但是如果我将 js 脚本分成自己的文件,则会出现以下错误,

由于 MIME 类型(“text/html”)不匹配(X-Content-Type-Options:nosniff),来自“ http://localhost:9000/userProfileFunctions.js ”的资源被阻止。

如果我将所有内容都保存在同一个 html 文件中,它会起作用,但这更像是解决问题的创可贴。我什至将 express app.use 标头设置为“X-Content-Type-Options: nosniff”,但它仍然不起作用

主文件

<html>

<head>
    <script
            src="https://code.jquery.com/jquery-3.4.1.min.js"
            integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
            crossorigin="anonymous">
    </script>

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

</head>
<body>
    <form>
        <h4>GET REQUEST USERS PROFILE</h4>
        UUID: <input id="getUserInput" type="text" name="UUID"><br>
        <input id="getUserProfile" type="button" value="submit">
    </form>
</body>
</html>

Run Code Online (Sandbox Code Playgroud)

应用程序.js

app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
});
Run Code Online (Sandbox Code Playgroud)

smo*_*re4 5

这不是一个非常技术性的答案,但即使生产版本有效,我在测试时也遇到了同样的错误。当我关闭 Firefox(开发者版)中的增强跟踪保护时,错误消失了。

在此输入图像描述


Abd*_*bdo 0

将该 JS 文件放在与“导入”html 文件相同的目录中。我刚刚解决了同样的问题。