代码之家  ›  专栏  ›  技术社区  ›  Italy Zia

我收到一个“拒绝执行来自的脚本”http://localhost:3000/js/dashboard/dashboard.js',因为其MIME类型('text/plain')不可执行

  •  0
  • Italy Zia  · 技术社区  · 1 年前

    我的代码在使用时有效

    <script> //my js code </script>

    但我试图将我的JavaScript代码放在ejs(HTML)文件之外,

    • 我创建了新的js文件,并将代码放入其中

    • 我导入到我的ejs文件

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

    我的浏览器显示此错误:

    GET http://localhost:3000/js/dashboard/dashboard.js net::ERR_ABORTED 404 (Not Found)

    dashboard:1 Refused to execute script from 'http://localhost:3000/js/dashboard/dashboard.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.

    当我进入poster并执行http GET请求以查看标题(内容类型)时:我注意到:

    postman GET request

    如何更改MIME内容类型,或者只是简单地避免此MIME错误。

    enter image description here

    这是我的arichitecure:

    enter image description here

    试图避免MIME错误

    0 回复  |  直到 1 年前
        1
  •  1
  •   jQueeny    1 年前

    我要冒险了,猜你是在为你的应用程序提供服务 /public 目录你的某些东西 app.js 沿着以下路线:

    app.use(express.static(__dirname + '/public'));`
    

    app.use('/static', express.static(path.join(__dirname, 'public')));
    

    如果是这样,那就意味着你想要的任何JavaScript 前端 要使用,如DOM操作和添加事件侦听器等,需要位于同一位置 /公共的 目录从你的文件布局来看,它会出现 dashboard.js 在它自己的范围内 /js 目录这意味着它只对上的Node.js可用 后端

    作为一个简单的解决方案,只需创建一个新文件夹: /public/js 并将 dashboard.js 在那里。

    为了清晰起见,请将您的文件移到此处:

    /public/js/dashboard.js

    更改您的 <script> 标记到:

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

    这个 MIME type ('text/html') 是转移注意力,因为如果你真的查看响应,很可能会有一些错误信息。。。以HTML形式呈现。