外部文件中的JS脚本:ReferenceError:函数未定义

时间:2020-02-11 11:09:14

标签: javascript html

我有以下问题: 这是我的register.html文件,其中包含注册脚本

<html lang="en">
<head>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
          integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <title>Customer registration</title>
    <script type="text/javascript" src="register.js"></script>
</head>
<body>



        <legend>Customer Registration</legend>
        <div class="form-group">
            <label class="col-md-4 control-label">First Name</label>
            <div class="col-md-4">
                <input id="firstname" name="firstname" type="text" placeholder="First Name" class="form-control input-md" required="">
            </div>
        </div>

        <div class="form-group">
            <label class="col-md-4 control-label">Last Name</label>
            <div class="col-md-4">
                <input id="lastname" name="lastname" type="text" placeholder="Last Name" class="form-control input-md" required="">
            </div>
        </div>

        <div class="form-group">
            <label class="col-md-4 control-label" for="email">Email</label>
            <div class="col-md-4">
                <input id="email" name="email" type="text" placeholder="example@example.com" class="form-control input-md" required="">
            </div>
        </div>

        <div class="form-group">
            <label class="col-md-4 control-label" for="password">Password</label>
            <div class="col-md-4">
                <input id="password" name="password" type="password" placeholder="password" class="form-control input-md" required="">
            </div>
        </div>

        <div class="form-group">
            <label class="col-md-4 control-label"></label>
            <div class="col-md-8">
                <input type="button" value="Register" id="registerBtn" onclick="prepareRegisterData()" class="btn btn-info btn-block">
            </div>
        </div>



</body>
<script type="text/javascript">
    function prepareRegisterData() {
        let registerData = {
            firstname: $('#firstname').val(),
            lastname: $('#lastname').val(),
            email: $('#email').val(),
            password: $('#password').val()
        };
        register(registerData);
    }

    function register(data) {
        $.ajax({
            url: "http://localhost:8080/customer",
            type: "POST",
            contentType: "application/json",
            data: JSON.stringify(data),
            success: function() {
                alert("Successfully registered");
            },
            error: function() {
                alert("Registration error")
            }
        })
    }
</script>
</html>

一切正常,但是我想用我的函数创建js文件。 我将脚本导入register.html中的head标签之间:

<script type="text/javascript" src="register.js"></script>

并将脚本从html中的脚本标签移动到register.js文件:

function prepareRegisterData() {
    let registerData = {
        firstname: $('#firstname').val(),
        lastname: $('#lastname').val(),
        email: $('#email').val(),
        password: $('#password').val()
    };
    register(registerData);
}

function register(data) {
    $.ajax({
        url: "http://localhost:8080/customer",
        type: "POST",
        contentType: "application/json",
        data: JSON.stringify(data),
        success: function() {
            alert("Successfully registered");
        },
        error: function() {
            alert("Registration error")
        }
    })
}

现在,如果我在控制台中单击“注册”按钮

ReferenceError:未定义prepareRegisterData

也许您知道我在这里错过了什么?

1 个答案:

答案 0 :(得分:1)

好的,项目结构是原因。 我在WEB-INF目录下有我的.js文件,现在我在WEB-INF以上有js目录,并且一切正常 感谢您的帮助:)