Here I am sending data from servlet to jsp using ajax request.But i didn't get any value from servlet on my client side.This is my code
这里我使用ajax request将数据从servlet发送到jsp。但是我没有从客户端的servlet获取任何值。这是我的代码
Servlet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String s="msg";
System.out.print("Servlet");
response.setContentType("text/plain");
PrintWriter out=response.getWriter();
out.print(s);
out.flush();
out.close();
}
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>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<title>Insert title here</title>
<script src="js/jquery-1.11.1.js" type="text/javascript"></script>
<script type="text/javascript">
function poll() {
setTimeout(function () {
$.ajax({
type: 'POST',
url: 'http://localhost:8080/ajaxtest/testajax',
success: function (data) {
alert(data); //DatO ANY PROCESS HERE
//document.getElementById("testid").value=data;
//document.write(data)
},
complete: poll
});
}, 5000);
}
</script>
</head>
<body onload="poll()">
<form><input type="text" name="test" id="testid" value=""></form>
</body>
</html>
output:
Am getting empty alert box and "No element found" on the browser debugging tool.
我在浏览器调试工具上得到空警报框和“找不到元素”。
1 个解决方案
#1
0
Your code is working fine for me. Check if you have your jquery.js accessed properly from jsp and also check your web.xml for correct servlet mapping. best to include below line for jquery.js , one less thing to bother about:
你的代码对我来说很好。检查是否从jsp正确访问了jquery.js,并检查web.xml以获取正确的servlet映射。最好包括jquery.js的下面一行,少打扰一下:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
#1
0
Your code is working fine for me. Check if you have your jquery.js accessed properly from jsp and also check your web.xml for correct servlet mapping. best to include below line for jquery.js , one less thing to bother about:
你的代码对我来说很好。检查是否从jsp正确访问了jquery.js,并检查web.xml以获取正确的servlet映射。最好包括jquery.js的下面一行,少打扰一下:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>