简体   繁体   English

未捕获的SyntaxError:无效或意外的令牌弹簧启动

[英]Uncaught SyntaxError: Invalid or unexpected token spring boot

Hi I have spring boot web app and I'm trying to add really simple javascript script to my index.html that redirects to another html on button click. 嗨,我有春季启动网络应用程序,我正在尝试添加非常简单的javascript脚本到我的index.html ,重定向到按钮单击上的另一个HTML。

This is my index.html: 这是我的index.html:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
    <head>
        <title>Dashboard</title>        
        <link rel="stylesheet" th:href="@{/css/bootstrap.css}" href="../css/bootstrap.css"/> 
        <link rel="stylesheet" th:href="@{/css/style.css}" href="../css/style.css"/>    
    </head>
    <body>
        <div class="container">
            <h1 th:inline="text">My Custom CMS Dashboard</h1>

            <a href="/addNewPost"><button class="btn btn-primary block"> Add new Post </button></a>

            <table class="table posts">
                <tr th:each="post : ${posts}" class="post">
                    <td th:text="${post.title}" class="title"></td>
                    <td th:text="${post.text}" class="text"></td>
                    <td th:text="${post.user.username}" class="username"></td>
                    <td>
                        <button class="btn block" id="editPostButton" onClick="redirectToEditPost()"> Edit Post </button>
                    </td>
                    <td>
                        <input id="postId" th:text="${post.id}" type="hidden" style="display:none"/>
                    </td>
                </tr>
            </table>

        </div>

        <script type="text/javascript" th:src="@{/webjars/jquery/3.2.1/jquery.min.js/}"></script>
        <script type="text/javascript" th:src="@{/js/editPost.js}"></script>
    </body>
</html>

And this is my javascript file: editPost.js: 这是我的javascript文件:editPost.js:

$(document).ready(function () {
    function redirectToEditPost(){
        window.location = '/editPost/' + $('input#postId').val();
    }
}

I keep getting this error: Uncaught SyntaxError : Invalid or unexpected token 我一直收到此错误: Uncaught SyntaxError :无效或意外的令牌

I'm pretty sure that it's some weird bug ( coughs JavaScript coughs ) and not actual syntax error since I saw similar posts that had weird issues like cashing of index.html or something like that. 我很确定这是一些奇怪的错误( 咳嗽 JavaScript 咳嗽 )而不是实际的语法错误,因为我看到类似的帖子有奇怪的问题,如index.html的兑现或类似的东西。

Can anyone help me figure this out since I'm not really in good terms with JavaScript errors or debugging JavaScript or anything related to JavaScript 任何人都可以帮我解决这个问题,因为我与JavaScript错误或调试JavaScript或与JavaScript相关的任何内容都不是很好

I'm aware that this might be something dealing with Spring Boot configuration or something other than JavaScript so I'm leaving spring-mvc tags... 我知道这可能是处理Spring Boot配置或JavaScript以外的东西所以我要离开spring-mvc标签......

I think you missing ); 我想你错过了); after your script code like below:- 在您的脚本代码如下: -

在此输入图像描述

$(document).ready(function () {
    function redirectToEditPost(){
        window.location = '/editPost/' + $('input#postId').val();
    }
});

Adding an event handler directly to a button is not best practice. 直接向按钮添加事件处理程序不是最佳做法。

Leave the button as is: 保持按钮不变:

<button class="btn block" id="editPostButton"> Edit Post </button>

Your document ready could then add the event handler to the button like so since you are already using jQuery: 然后,您准备好的文档可以将事件处理程序添加到按钮中,因为您已经在使用jQuery:

$(document).ready(function () {
        $('#editPostButton').on('click', function () {
                window.location = '/editPost/' + $('input#postId').val();
        });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM