【问题标题】:Importing .js file into html using <script>使用 <script> 将 .js 文件导入 html
【发布时间】:2020-07-01 20:44:12
【问题描述】:

我查看了 StackOverflow 上的不同帖子,但尽管遵循了建议,但我似乎无法解决这个问题。任何帮助将不胜感激。

我的前端文件夹中的 home.html 文件中有以下 HTML 代码:

`<!DOCTYPE html>
  <html lang="en">
  <head>
  <title> Home Page </title>
   <meta charset="utf-8">
   <script src ="src/home.js"></script>
  </head> 
  </html>`

我在尝试导入的文件夹 src 中有一个 home.js 文件。但是,当我运行 localhost 时,我得到了错误:

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

我已经尝试添加

app.use("/src", express.static(path.join(__dirname, "../src"))); app.use("/frontend", express.static(path.join(__dirname, "../frontend")));

到我的 server.js 文件,但它没有帮助。

提前致谢!

【问题讨论】:

标签: javascript html


【解决方案1】:

与“类型”属性一起添加,如下所示。

<script type="text/javascript" src="path-to-javascript-file.js"></script>

【讨论】:

  • 告诉浏览器期待text/javascript响应,服务器的HTTP响应头仍然是权威的。
猜你喜欢
  • 2014-10-20
  • 2021-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多