【发布时间】:2014-09-25 16:08:46
【问题描述】:
这里我使用 ajax 请求将数据从 servlet 发送到 jsp。但是我没有从客户端的 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>
输出:
在浏览器调试工具上出现空警报框和“未找到元素”。
【问题讨论】:
-
请求是否正确发送?网络控制台说什么?
-
我已经上传了输出截图
-
你有注册的过滤器吗?
-
不。这是我尝试的全部代码..
-
只需在 servlet 的
post()方法中放入一个 SOP 并确认 servlet 正在正确调用。只需使用url: 'testajax',
标签: java jquery ajax jsp servlets