Java 未加载消息时发生Ajax错误

Java 未加载消息时发生Ajax错误,java,javascript,jquery,ajax,servlets,Java,Javascript,Jquery,Ajax,Servlets,当我单击login.html上的提交按钮时,我收到一个未加载消息的错误。我在这方面没有太多的经验,也没有错误消息告诉我修复它的方向。 我有我的login.html <body> <div id="log"> <h2>Login</h2> <form method="post" action="login" class="form-1" accept-charset="utf-8" onsubmit="sendAjax(

当我单击login.html上的提交按钮时,我收到一个未加载消息的错误。我在这方面没有太多的经验,也没有错误消息告诉我修复它的方向。 我有我的login.html

<body>
    <div id="log">
    <h2>Login</h2>
    <form method="post" action="login" class="form-1" accept-charset="utf-8" onsubmit="sendAjax()">
        <input type="text" name="username" id="username" placeholder="Username"/><br/>
        <input type="password" name="password" id="password" placeholder="Password"/><br/>
        <input type="submit" value="Sign In"/>
    </form>
    </div>
</body>
我的登录servlet

    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    String username = request.getParameter("username");
    String password = request.getParameter("password");

    if(databaseConnection.checkUser(username, password))
    {
        RequestDispatcher rs = request.getRequestDispatcher("Welcome");
        rs.forward(request, response);
    }
    else
    {
        out.println("Username or Password incorrect");
        RequestDispatcher rs = request.getRequestDispatcher("login.html");
        rs.include(request, response);
    }
}
还有我的ajax函数

function sendAjax(){
    var article = new Object();
    article.username = $('#username').val();
    article.password = $('#password').val();

    $ajax({
        url: "http://localhost:8080/FishingTrax/LoginServlet",
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(article),
        contentType: 'application/json',
        mimeType: 'application/json',

        success: function (data) {

        },
            error:function(data,status,er) {
                alert("error: "+data+" status: "+status+" er:"+er);
                }
    });
};

我的代码有两个问题,在sendAjax()之前缺少返回。define Ajax问题是因为$Ajax中缺少一个句号,所以应该是$.Ajax

function sendAjax(){
var article = new Object();
article.username = $('#username').val();
article.password = $('#password').val();

$.ajax({
    url: "http://localhost:8080/FishingTrax/LoginServlet",
    type: 'POST',
    dataType: 'json',
    data: JSON.stringify(article),
    contentType: 'application/json',
    mimeType: 'application/json',

    success: function (data) {

    },
        error:function(data,status,er) {
            alert("error: "+data+" status: "+status+" er:"+er);
            }
});

})

onsubmit=“return sendAjax()”
(或者按照规定在头部添加事件处理程序)并在sendAjax中添加一个
return false结束。此外,您还为不存在的
$(“#login”)
分配了一些内容。我将onsubmit更改为具有return sendAjax(),并将return false添加到ajax中。登录是我菜单中的一个按钮,它将login.html加载到我的index.html页面上的一个div中。遗憾的是,我仍然收到加载页面错误的问题,但我正在将脚本加载到我的索引页面上,因为我知道在加载到另一个html页面上的div时可能会丢失该脚本。好的,因此我收到了未加载错误和引用错误:index.html中未定义sendAjax
function sendAjax(){
var article = new Object();
article.username = $('#username').val();
article.password = $('#password').val();

$.ajax({
    url: "http://localhost:8080/FishingTrax/LoginServlet",
    type: 'POST',
    dataType: 'json',
    data: JSON.stringify(article),
    contentType: 'application/json',
    mimeType: 'application/json',

    success: function (data) {

    },
        error:function(data,status,er) {
            alert("error: "+data+" status: "+status+" er:"+er);
            }
});