【问题标题】:uncaught SyntaxError: Unexpected token < line one js未捕获的 SyntaxError:意外的令牌 < 第一行 js
【发布时间】:2017-11-20 18:14:30
【问题描述】:

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

Uncaught SyntaxError: Unexpected token

它说错误在 js 文件的第一行,但源代码只在 html 文件的第一行显示红线。

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

谢谢!

这是我的 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);
        });
});
 
  });

  

【问题讨论】:

  • 我猜这是个愚蠢的问题,但是您是否在服务器上运行 ES6?
  • 为什么这个问题被标记为 go?它似乎与 go 没有任何关系。
  • 创建一个 plunker/fiddler/SO sn-p 重现上述错误
  • @Snowmonkey 不,它只是在本地主机上运行。我有一个提供上述 html 文件的 go 文件
  • 不确定,但我想知道您的script type "text/JavaScript" 是否会导致此问题。应该是 "text/javascript" -- 但在 HTML5 中应该完全省略,只使用 &lt;script&gt; 而不使用类型属性。

标签: javascript jquery html


【解决方案1】:

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

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

【讨论】:

  • 我不确定,因为我正在使用 go 文件在本地主机上提供该 html 页面。它非常基本,只有一个 main 函数 - func main() { // 处理根页面 http.HandleFunc("/", requestHandler) //处理/聊天页面 http.HandleFunc("/Chat", chatHandler) http.ListenAndServe (":8080", nil) }// 和 this 作为 requestHandler// - func requestHandler(w http.ResponseWriter, r *http.Request) { //提供 homepage.html 文件 http.ServeFile(w, r, "ChatBot.html") }// chatHandler 目前什么都不做
猜你喜欢
  • 1970-01-01
  • 2016-10-23
  • 2015-08-19
  • 2016-04-04
  • 2013-10-08
  • 1970-01-01
  • 2021-09-11
  • 1970-01-01
相关资源
最近更新 更多