【问题标题】:Error: friends.js:1 Uncaught SyntaxError: Unexpected token <, after using express.static and change html content after button click错误:friends.js:1 Uncaught SyntaxError: Unexpected token <, after using express.static and change html content after button click
【发布时间】:2017-01-04 16:10:36
【问题描述】:

我试图在 localhost:9000 的初始加载时显示 home.html,但是当我按照当前代码的方式执行时,我收到错误:friends.js:1 Uncaught SyntaxError: Unexpected token &lt;。我不确定这个错误是什么意思。此外,当我在 display.js 中执行 windows.location = localhost:9000/list 时,get 请求不会将 list.html 发送到浏览器,并且没有任何变化。我尝试将 get 请求放在 server.js 和 display.js 中,但它们都没有执行任何操作。

目录布局

dir main
    -server.js
    dir subMain
      dir display
        -display.js
      dir routing
        -routes.js
      dir public
        -home.html
        -list.html

server.js

var path = require('path');
var express = require('express');
var app = express();
require('./subMain/routing/routes.js')(app, path, express);

app.listen(9000, function(){
     console.log('connected on 9000')
})



//app.get('/list', function(request, response){
       // response.sendFile(path.join(__dirname + '/..', 'public', 'list.html'));
    //});

routes.js

module.exports = function(app, path, express){
    app.use(express.static("subMain"))
    app.use(express.static(__dirname + "/public"));
    app.use(express.static(__dirname + "/routing"));
    app.use(express.static(__dirname + "/display"));
    app.use(function(request, response, next){
      response.sendFile(path.join(__dirname + "/..", "public", "home.html"));
    })
    app.get('/list', function(request, response){
        response.sendFile(path.join(__dirname + '/..', 'public', 'list.html'));
    });

}

display.js

$(document).on('click', '#btn', sendSurvery);

function sendSurvery(){
    window.location = 'survey.html';
    //var myQueryUrl = "http://localhost:9000/survey";

    //$.ajax({url: myQueryUrl, method: 'GET', success: function(result){
        // location.href = "http://localhost:9000/list"

    //}}).done(function(response){

    //});
}

home.html

<!DOCTYPE html>
<html>
<head>
    <title>Friend Finder Home Page</title>

</head>
<body>
    <div class="rowOne">
        <!-- <div class="jumbotron col-lg-6"> -->
          <h1>Hello, world!</h1>
          <p>Click the button</p>
          <button id="btn" style="width: 200px;">BUTTON</button>
        <!-- </div> -->
    </div>
<script src="https://code.jquery.com/jquery.js"></script>
<script type="text/javascript" src="../data/friends.js"></script>
</body>
</html> 

【问题讨论】:

    标签: javascript node.js express


    【解决方案1】:

    您尚未提供您的home.html 文件,但您遇到的问题是您的friends.js 没有被express 找到,而是返回您的home.html 文件。 home.html 文件中的第一个字符是 &lt;,这就是您收到错误的原因。检查您是否引用了正确的 friends.js 路径,并且它存在于您的静态资产文件夹中。

    为了确认我的解释,你可以直接访问你用来访问friends.js的url,然后查看返回的home.html的内容。

    【讨论】:

    • 我没有提供 home.html 是什么意思?而且当我做express.static时,路径是相对的还是从使用节点运行服务器的位置开始,这将是server.js?因此,即使我的 express.static 代码位于路径 (server.js)/subMain/routing/routes.js 中,它也是从 server.js 的位置开始的?如果是这样,我将不得不将我的 express.static 路径更改为“/submain/routing”?
    • 我的意思是你没有在你的问题中输入home.html。根据文档使用__dirname 来获取您当前的工作目录。从那里你可以制定出你的路径。 nodejs.org/docs/latest/api/globals.html#globals_dirname 请求friends.js 时收到的响应是否查看过?
    • 好吧,我知道friends.js 已正确链接到我的home.html,因为hte url 从我的代码中的windows.location 发生了变化。但我现在包含了有问题的 home.html 代码。就单击按钮后显示 list.html 而言,没有任何反应。我之前提到的关于&lt; 的错误不再出现,但每次我取出代码app.use(express.static("subMain")) 时都会再次出现该错误。
    • 为了将我的回答标记为已接受,请在收到Uncaught SyntaxError: Unexpected token &lt; 时查看friends.js 响应正文,并看到您收到的是HTML,而不是javascript。
    • 当我执行app.use(express.static("subMain")) 时,我不再收到该错误。 Friends.js 不是通过 URL 访问的,它链接到我的 home.html,除非您指的是主页 localhost:9000 的 url。 Friends.js 只是有一个点击事件,将 windows.location 更改为 localhost:9000/list,但即使它发生了变化,get 请求也不起作用。如何查看响应正文?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 2017-11-16
    • 2015-04-21
    • 2018-02-04
    • 2016-09-19
    • 2017-11-24
    相关资源
    最近更新 更多