未捕获的SyntaxError:意外令牌<第一行js [英] uncaught SyntaxError: Unexpected token < line one js

查看:57
本文介绍了未捕获的SyntaxError:意外令牌<第一行js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里看到了许多与此问题类似的问题,但是似乎没有一个问题可以解决我遇到的问题. 我不断收到以下错误-

I have seen many questions similar to this one on here but none of them seem to fix the problem I am having. I keep getting the following error-

未捕获到的SyntaxError:意外令牌<

Uncaught SyntaxError: Unexpected token <

它说错误在js文件的第一行中,但是源仅在html文件的第一行中显示了一条红线.

It says the error is in line one of the js file but the sources only shows a red line on line one of the html file.

还应该提到它正在运行在go服务器上.

Should also mention it is running on a go server.

谢谢!

这是我的HTML

<!DOCTYPE html>
<html lang="en">

<head>
    <title>Hello, world!</title>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Sourcing jquery and ajax -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

    <!--external script.js file-->
    <script type = "text/JavaScript" src="ChatBot.js"></script>

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
    <style>
        body {
            padding-top: 65px;
            padding-bottom: 100px;
        }
    </style>

</head>

<body>
    <nav class="navbar fixed-top navbar-dark bg-dark">
        <a class="navbar-brand" href="#">Eliza chat bot</a>
    </nav>
    <div class="container-fluid">
        <ul class="list-group">
	 <div class="fixed-bottom bg-dark p-2">
                <div class="container">
                    <div>
                        <ul class="list-group"></ul>
                    </div>
                    <form action="/Chat" method="GET">
                        <div class="form-group">
                            <input type="text" class="form-control" id="userInput" placeholder="Talk to eliza...">
                        </div>
                    </form>
                </div>
            </div>

        </ul>  </div>
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>

    
</body>

</html>

这是我的js

const form = $("#userInput");
const listItem = $("#list-group");

//add the keypress function for when the user types an input for eliza to compute

$(document).ready(()=>{

form.keypress(function(event){

    const keyCodes = {
    ENTER : 13
}

let keyCode = event.keyCode;

    //for enter
    if(event.keyCode !=  keyCodes.ENTER){ 
        return;
    }

      event.preventDefault(); 

      const input = form.val();
      form.val(" ");

       listItem.append("<li class='list-group-item list-group-item-success'>"+"User : " + input + "</li>");

      // GET/POST
    const queryParams = {"userInput" : input }
    $.get("/Chat", queryParams)
        .done(function(response){
            var nextListItem = "<li class='list-group-item list-group-item-info'>"+"ELiza : " + response + "</li>";
            setTimeout(function(){
                listItem.append(nextListItem)
            }, 1000);//set timeout to give wait to response
        }).fail(function(){
            var nextListItem = "<li class='list-group-item list-group-item-danger' >Sorry I'm not home right now.</li>";
            list.append(nextListItem);
        });
});
 
  });

  

推荐答案

您的网络服务器是否有一条规则,如果未找到所请求的资源,它将默认为index.html文件提供服务?这是单页应用程序的常用配置.

Does your webserver happen to have a rule where if the requested resources is not found it will default to serving the index.html file? This is a popular configuration for Single Page Applications.

在这种情况下,如果服务器找不到请求的js文件,它将提供html文件的内容,浏览器将尝试将其解析为JavaScript.由于html文件第一行的第一个字符为<,因此您将收到语法错误,因为该错误不是有效的JavaScript.

If that is the case, if the server can not find the js file requested it will serve the contents of the html file, which the browser will try parse as JavaScript. Since the first character on the first line of the html file is < you'll get a syntax error since that is not valid JavaScript.

这篇关于未捕获的SyntaxError:意外令牌&lt;第一行js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆