0

我不是 Java EE 和 Ajax 的优秀开发人员,当我从 A Servlet 获取信息时遇到问题:

这是我的 JS 文件:

            $password = document.getElementById("pass").value;
            $name = document.getElementById("name").value;
            var $hash="";
            if ( $password  != '') {
                $hash = hex_md5($password);
            }
            $.ajax({
                url: 'identification',
                type: 'POST',
                data:{pass:$hash,name:$name},
                dataType: 'html',
                timeout: 1000,
                success: function(data){
                    alert(data);

                    if(data=='no'){
                                    $("#messages").html("Authentication prob ");
                                  }
                    else if(data=='db'){
                        $("#messages").html(" DataBase prob ");
                      }
                    else
                    {
                        $("body").html(data);
                        //or i want to redirect to new page
                    //window.location ="/BookListServlet?nom="+$nom;
                    }
                }
            });

这是我的 Servlet:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub

    String name= request.getParameter("name");
    String pass=request.getParameter("pass");

    if (name.length()==0)
        {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.println("no");
            out.flush();
            out.close();
        }

    else if (name.equals("admin")&&pass.equals("0cc175b9c0f1b6a831c399e269772661"))
    {
        if(bookiml.get_connection()!=null)//db is ok
        {
            getServletContext().getRequestDispatcher("/BookListServlet").forward(request, response);
        }
        else
        {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.println("db");
            out.flush();
            out.close();
        }

    }

    else 
    {
     response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("no");
        out.flush();
        out.close();
    }
}

我有一个大问题:重定向页面并从 Servlet 获取 HTML 内容

我需要将发送标识的请求转发到此booklistservlet,然后从 Servlet 获取页面。

为什么这段代码不起作用?

4

1 回答 1

0

好的,这data实际上不是您从服务器返回的直接值,而是一个 html 页面。(服务器的响应)。因此,您必须data使用 JQuery 来操作,您可以简单地做到这一点

var password = $('#pass').val();
var name = $('#name').val();
var hash="";
if ( password  != '') {
    hash = hex_md5(password);
}
$.ajax({
    url: 'identification',
    type: 'POST',
    data:{
        pass:hash,
        name:name
    },
    dataType: 'html',
    timeout: 1000,
    success: function(data){
        alert(data);

        if($(data).find('body').text()=='no'){
            $("#messages").html("Authentication prob ");
        }
        else if($(data).find('body').text()=='db'){
            $("#messages").html(" DataBase prob ");
        }
        else
        {
            $("body").html($(data).find('body').text());
        //or i want to redirect to new page
        //window.location ="/BookListServlet?nom="+$nom;
        }
    }
});

附言

出于记录目的,我更喜欢使用console.log();而不是警报框。

于 2012-08-09T17:54:58.550 回答