简体   繁体   English

未捕获的语法错误:ajax 调用中的无效或意外标记

[英]Uncaught SyntaxError: Invalid or unexpected token in ajax call

I have a form which looks like this:我有一个看起来像这样的表格:

<form id="login_form" class="border shadow p-3 rounded" method="post" style="width: 450px;">
        <h1 class="text-center p-3">LOGIN</h1>

        <div id="error_box" class="alert alert-danger" role="alert"> </div>

        <div class="mb-3">
            <label for="username" class="form-label">User name</label>
            <input type="text" class="form-control" name="username" id="username">
        </div>

        <div class="mb-3">
            <label for="password" class="form-label">Password</label>
            <input type="password" name="password" class="form-control" id="password">
        </div>

        <button type="submit" class="btn btn-primary">LOGIN</button>
</form>

When I click the login button I want to check if the given username and password are in the database.当我单击登录按钮时,我想检查给定的用户名和密码是否在数据库中。 If they are, I want to move to a different page.如果是,我想转到另一个页面。 If they are not, I add an error message text to the above div with ID "error_box" and make it visible (it is initially invisible).如果它们不是,我将错误消息文本添加到上面的 ID 为“error_box”的 div 并使其可见(它最初是不可见的)。 This is how the jquery code looks like:这是 jquery 代码的样子:

<script>
        $(document).ready(function () {
            const errorBox = $("#error_box");
            errorBox.hide();
            $("form").on('submit', function (event) {
                errorBox.empty();
                $.ajax({
                    type: "GET",
                    url: "controller/controller.php",
                    data: {action: "checkLogin", username: "<?=$_POST['username']?>", password: "<?=$_POST['password']?>"},
                    success: function (jsonResponse) {
                        const result = $.parseJSON(jsonResponse)["result"];
                        if (result === "success") {
                            window.location.href = "home.php";
                        } else {
                            errorBox.show();
                            errorBox.append(result);
                        }
                    }
                });
                event.preventDefault();
            });
        })
</script>

The function checkLogin from controller.php will return a json with one field, "result".来自controller.php checkLogin返回带有一个字段“结果”的 json。 If the username/password combination is correct, it will return {"result": "success"}.如果用户名/密码组合正确,它将返回 {"result": "success"}。 If the username/password combination is incorrect, it will return {"result": <error_message>}, where <error_message> can be something like "You cannot enter an empty username".如果用户名/密码组合不正确,它将返回 {"result": <error_message>},其中 <error_message> 可以是类似“您不能输入空用户名”的内容。 You get the idea.你明白了。

The problem is that I get an "Uncaught SyntaxError: Invalid or unexpected token".问题是我收到“未捕获的语法错误:无效或意外的标记”。 The browser says it is at the line where I have type: "GET" in the ajax call.浏览器说它在我type: "GET" This exception also makes the div which will have the error message to not be hidden, so I have an empty red box above the username and password fields (and it shouldn'y be there, it should only appear after the user clicks on the login button with invalid username/password).这个异常也使得将有错误消息的 div 不被隐藏,所以我在用户名和密码字段上方有一个空的红色框(它不应该在那里,它应该只在用户点击登录后出现用户名/密码无效的按钮)。 If I delete the ajax call I don't have the exception anymore and the div gets hidden.如果我删除 ajax 调用,我将不再有异常,并且 div 会被隐藏。 I don't know how to solve it/why I get this exception.我不知道如何解决它/为什么我得到这个异常。 I tried specifying the dataType attribute in the ajax call as 'json' , but that still didn't work.我尝试在 ajax 调用中将dataType属性指定为'json' ,但这仍然无效。 I tried changing the ajax call to a $.getJSON() call and I got the same exception (I know they do the same things, but I'm getting desperate).我尝试将 ajax 调用更改为$.getJSON()调用,但我遇到了同样的异常(我知道他们做同样的事情,但我越来越绝望了)。 What's wrong?怎么了?

There is php syntax in your ajax form and the problem is in those lines where you get the username and password from the inputs.在您的 ajax 表单中有 php 语法,问题出在您从输入中获取用户名和密码的那些行中。 Try to get the values of inputs from using jquery or javascript.尝试使用 jquery 或 javascript 获取输入值。

And your script should be like this你的脚本应该是这样的

<script>
    $(document).ready(function () {
        const errorBox = $("#error_box");
        errorBox.hide();
        $("form").on('submit', function (event) {
            errorBox.empty();
            $.ajax({
                type: "GET",
                url: "controller/controller.php",
                data: {action: "checkLogin", username: $('#username').val(), password: $('#password').val()},
                success: function (jsonResponse) {
                    const result = $.parseJSON(jsonResponse)["result"];
                    if (result === "success") {
                        window.location.href = "home.php";
                    } else {
                        errorBox.show();
                        errorBox.append(result);
                    }
                }
            });
            event.preventDefault();
        });
    })

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

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