sentData是测试发送的数据但是在servlet里接收不到,为什么?
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<script type="text/javascript">
function _commit() {
var sentData = {"name":"xxx","password":"我不好"};
$.ajax({
url:"${pageContext.request.contextPath}/ParseJsonServlet", //访问路径
type:"POST", //访问方式
data:sentData, //传入服务端的数据
dataType:"json",
contentType:"application/json;charset=utf-8",
success :function() {
$(document.forms[0]).remove();
$("#statuSpan").text("提交成功!");
}
});
}
</script>
<script type="text/javascript" src="js/jquery-1.5.1.js"></script></head>
<body>
<form action="" method="POST">
<input type="text" name="name" value="Jack">
<input type="text" name="description" value="My name is Jack">
<input type="button" value="提交" onclick="_commit()">
</form>
<span id="statuSpan" ></span>
</body>
</html>
//================Servlet代码=================
public class ParseJsonServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
System.out.println(request.getParameter("name"));
}
}
4 个解决方案
#1
试试用FF等调试工具 看看发送到后台的数据长啥样
#2
用以下代码可以获取,但中文显示不出来
BufferedReader bufr =
new BufferedReader(
new InputStreamReader(request.getInputStream(),"UTF-8"));
StringBuilder sBuilder = new StringBuilder("");
String temp = "";
while((temp = bufr.readLine()) != null){
sBuilder.append(temp);
}
bufr.close();
String json = sBuilder.toString();
System.out.println(json);
输出结果为:name=xxx&description=%E6%88%91%E4%B8%8D%E5%A5%BD
#3
应该是乱码了。。好像java要调用2次encodeURIComponent来编码数据吧。。具体不懂java如何解码了
#4
我找到两者解决方法
第一种是将 $.ajax()里的 contentType:"application/json;charset=utf-8", 注释掉
这样就可以通过
第二种是将sentData加单引号改为 var sentData =‘ {"name":"xxx","password":"我不好"}’;
这样可以通过
第一种是将 $.ajax()里的 contentType:"application/json;charset=utf-8", 注释掉
这样就可以通过
获取json参数值了
request.setCharacterEncoding("UTF-8");
System.out.println(request.getParameter("name"));
第二种是将sentData加单引号改为 var sentData =‘ {"name":"xxx","password":"我不好"}’;
这样可以通过
获取参数列表
BufferedReader bufr =
new BufferedReader(
new InputStreamReader(request.getInputStream(),"UTF-8"));
StringBuilder sBuilder = new StringBuilder("");
String temp = "";
while((temp = bufr.readLine()) != null){
sBuilder.append(temp);
}
bufr.close();
String json = sBuilder.toString();
System.out.println(json);
#1
试试用FF等调试工具 看看发送到后台的数据长啥样
#2
用以下代码可以获取,但中文显示不出来
BufferedReader bufr =
new BufferedReader(
new InputStreamReader(request.getInputStream(),"UTF-8"));
StringBuilder sBuilder = new StringBuilder("");
String temp = "";
while((temp = bufr.readLine()) != null){
sBuilder.append(temp);
}
bufr.close();
String json = sBuilder.toString();
System.out.println(json);
输出结果为:name=xxx&description=%E6%88%91%E4%B8%8D%E5%A5%BD
#3
应该是乱码了。。好像java要调用2次encodeURIComponent来编码数据吧。。具体不懂java如何解码了
#4
我找到两者解决方法
第一种是将 $.ajax()里的 contentType:"application/json;charset=utf-8", 注释掉
这样就可以通过
第二种是将sentData加单引号改为 var sentData =‘ {"name":"xxx","password":"我不好"}’;
这样可以通过
第一种是将 $.ajax()里的 contentType:"application/json;charset=utf-8", 注释掉
这样就可以通过
获取json参数值了
request.setCharacterEncoding("UTF-8");
System.out.println(request.getParameter("name"));
第二种是将sentData加单引号改为 var sentData =‘ {"name":"xxx","password":"我不好"}’;
这样可以通过
获取参数列表
BufferedReader bufr =
new BufferedReader(
new InputStreamReader(request.getInputStream(),"UTF-8"));
StringBuilder sBuilder = new StringBuilder("");
String temp = "";
while((temp = bufr.readLine()) != null){
sBuilder.append(temp);
}
bufr.close();
String json = sBuilder.toString();
System.out.println(json);