【问题标题】:Ajax error not loading messageAjax 错误未加载消息
【发布时间】:2014-04-23 12:32:49
【问题描述】:

当我单击 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>

在我的 index.html 中被调用到一个 div 中

$('#login').click(function(e){ 
$('#map-canvas').addClass("hidden");
$('#dMain').removeClass("hidden");
$('#dMain').load('html/login.html');
e.preventDefault();

我的登录小服务程序

    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);
                }
    });
};

【问题讨论】:

  • onsubmit="return sendAjax()"(或按规定在头部添加事件处理程序)并在 sendAjax 中添加 return false; 到末尾。你也给不存在的$('#login')分配了一些东西
  • 我添加了将 onsubmit 更改为返回 sendAjax() 并将 return false 添加到 ajax 中。登录是我菜单中的一个按钮,它将 login.html 加载到我的 index.html 页面上的 div 中。可悲的是,我仍然收到错误加载页面问题,但我正在索引页面上加载脚本,因为我知道在加载到另一个 html 页面上的 div 时可能会错过。
  • 好的,所以我收到错误未加载和引用错误:sendAjax 未在 index.html 中定义

标签: java javascript jquery ajax servlets


【解决方案1】:

我的代码有两个问题,我在 sendAjax() 之前缺少返回。定义 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);
            }
});

};

【讨论】:

    猜你喜欢
    • 2015-01-15
    • 2014-11-30
    • 2015-06-01
    • 2023-04-06
    • 2019-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多