ajax-login servlet:servlet没有回答ajax代码

时间:2014-04-19 11:22:36

标签: jquery ajax servlets login

我有一个登录表单(名称和密码),我希望当用户输入他的名字和密码时,要执行一个ajax代码并调用一个进行验证的servlet,它会产生结果(成功或失败)

这是我的表单的代码:

<form action="" method="GET" id="login_frm">
        <label ><b>Username: </b></label>
        <input class="field" type="text" id="log" value="" size="23" />
        <label ><b>Password:</b></label>
        <input class="field" type="password"  id="pwd" size="23" />
        <input type="submit"  value="" class="button_login" />
</form>

这是ajax代码:

$(document).ready(function(){

        $("#login_frm").submit(function(){
        $.ajax({
            type: 'GET',
            url: '/RecommandationDefault/connexion?user='+$('#log').val()+'&pwd='+$('#pwd').val(),
            success: function(msg){
                alert(msg);
              }
          });
        });
            ........

    }

当我点击登录按钮,servlet执行:out.write(“ok”)时,我简化了servlet的处理,就像命运一样,我做了一个ajax警报,以回应验证通信的发生。

我的servlet代码(connexion.java)

@WebServlet(name = "connexion")
public class connexion extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

        } finally {
            out.close();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
         PrintWriter out = response.getWriter();
        System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");

        out.write("ok");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

问题:Consul出现了^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^^ ^ 但是没有触发警报。

感谢

1 个答案:

答案 0 :(得分:0)

您没有从servlet返回任何内容。尝试使用alert中的任何硬编码值而不是msg

success: function(msg){
           alert("Done");
        }