管理從回調函數數據(HTML)我在Java EE和阿賈克斯不是一個好developper,我有一個問題,當我從一個servlet獲取信息:如何用jQuery
這是我的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中獲取一個頁面。
爲什麼這段代碼不工作?
你不工作是什麼意思? – 2012-08-09 17:02:49
感謝您的回答,我的意思是$(「#messages」)。html(...)和$(「body」)。html(data);不工作,但警報(數據)工作 – Spl2nky 2012-08-09 17:26:29
另外,JavaScript加密不受歡迎。 http://www.matasano.com/articles/javascript-cryptography/ – 2012-08-09 18:04:25