首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将JavaScript文件导入HTML文件

将JavaScript文件导入HTML文件
EN

Stack Overflow用户
提问于 2018-08-27 04:00:04
回答 1查看 488关注 0票数 -1

我有一个很大的问题,链接JavaScript文件到超文本标记语言文件。

我的HTML代码:

代码语言:javascript
复制
<!DOCTYPE html>
<html>
    <head>
        <title>Express App</title>
    </head>
    <body>
        <h1>GET Requests</h1><br/>
        <input type="number" name="id" id="id-input>
        <button type="submit" onclick="findPerson()">Submit</button>
        <script src="./index.js" type="text/javascript"></script>
    </body>
</html>

这段代码看起来很好,实际上,下面是我的JavaScript代码:

代码语言:javascript
复制
const express = require('express');
const app = express();
const server = 'http://localhost:3000';
app.connect(server);
app.get('/', (req, res) => {
    res.sendFile(__dirname + "/" + "index.html");
});

const findPerson = () => {
    var ID = window.document.getElementById('id- 
    input').value;
    console.log(ID);
};

app.listen(8080, () => {
    console.log('App is listening on port 8080');
});

现在,出于某种原因,当我检查页面并转到控制台时,它显示如下:

代码语言:javascript
复制
Refused to execute script from 
'http://localhost:8080/index.js' because 
its MIME type ('text/html') is not executable, and 
strict MIME type checking is enabled.

还有这个:

代码语言:javascript
复制
Failed to load resource: the server responded with 
a status of 404 (Not Found) index.js:1
EN

回答 1

Stack Overflow用户

发布于 2018-08-27 04:05:51

我可以在您的代码中看到两个错误:

  • 404错误意味着您没有链接到正确的路径,请检查您的index.js相对于您的index.html
  • 的位置,即使它将加载它,您的代码也无法执行,因为它是服务器端代码。当您在浏览器中包含脚本时,应该只使用客户端代码
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52029819

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档