我是Jquery和ajax的新手,编写了一个简单的jsp页面,它将显示每个onkeyup事件的文本,ajax发出的请求将通过AjaxServlet。我没有使用.ajax()方法获得输出,而如果我使用简单的ajax使用XhtmlRequest它给出输出。 请作为sson尽可能
** * * JSP页面 * ** * ** *
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script>
function showName(str){
$.ajax({
url: '/AjaxServlet',
type: 'GET',
data: 'name='+name,
success: function(response){
$("#showName").html(response);
}
});
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<input type="text" onkeyup="showName(this.value)">Enter your name</input>
<p>Suggestions: <span id="showName"></span></p>
</body>
</html>
的 的 **** AjaxServlet 的 * ** < EM> * ** * *
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name=request.getParameter("name");
response.setContentType("text/xml");
PrintWriter responseWriter=response.getWriter();
responseWriter.write("Welcome "+name);
}
答案 0 :(得分:2)
你的jQuery库在哪里?您是否忘记下载并将其包含在HTML页面中?
答案 1 :(得分:2)
是'name'实际上是正确使用的东西吗?函数是showName(str)
?
尝试以下方法:
$.ajax({
url: '/AjaxServlet',
type: 'GET',
data: {name:str}, // use a object literal here
success: function(response){
$("#showName").html(response);
},
error: function(xhr, status, error){
alert("help I died! [" + status + "] [" + error +"]" );
}
});
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name=request.getParameter("name");
// your output isn't xml! ... use text/plain in stead
response.setContentType("text/plain");
PrintWriter responseWriter=response.getWriter();
responseWriter.write("Welcome "+name);
}