无法将“JSON.stringify”数据发送到servlet

时间:2016-12-14 07:48:44

标签: java json ajax jsp servlets

我无法以JSON格式从我的jspservlet控制器发送数据。我想,我在ajax调用servlet时出错了。以下是我的代码:

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 http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script src="js/jquery.min.js" type="text/javascript"></script>
<script>
var SendInfo=[];
$(document).ready(function(){
    $("#but").click(function(){
        $(".tabclass tr td input[type='checkbox']:checked").each(function(){
            var sibs=$(this).parent().siblings("td");

            var v0= $(this).val();

            var v1=$(sibs[0]).text(); 

            var v2=$(sibs[1]).children("select[name='address']").val();

            var domain = {
                    id:v0,
                    name: v1,
                    address: v2

            }
            SendInfo.push(domain);

            alert(v0+" & "+v1+" & "+v2);

        }); 
        $.ajax({
              type: "POST",
              url: "s1.do",
              data: JSON.stringify({ students: SendInfo }), // might be wrong here or is it correct way to send data?
              success: function(data, status){
                  alert("Data: " + data + "\nStatus: " + status);
              },
              error: function(XMLHttpRequest, textStatus, errorThrown) {
                 alert("some error");
              }
            });
    });
});
</script>
</head>
<body>
<table border="1" class="tabclass">
<th>select</th><th>Name</th><th>Address</th>
<tr>
<td><input type="checkbox" name="selectCheck" class="select" id="ch1" value="1"/> </td>
<td><span class="name">Nitin</span></td>
<td><select name="address">
<option>Gurgaon</option>
<option>Noida</option>
<option>Rohini</option>
</select></td>
</tr>
<tr>
<td><input type="checkbox" name="selectCheck" class="select" id="ch2" value="2"/> </td>
<td><span class="name">Abc</span></td>
<td><select name="address">
<option>Gurgaon</option>
<option>Noida</option>
<option>Rohini</option>
</select></td>
</tr>
<tr>
<td><input type="checkbox" name="selectCheck" class="select" id="ch3" value="3"/> </td>
<td><span class="name">Xyz</span></td>
<td><select name="address">
<option>Gurgaon</option>
<option>Noida</option>
<option>Rohini</option>
</select></td>
</tr>
</table><br><br>
<button id="but">Test</button>
<br></br><button id="but2">Test2</button>
</body>
</html>

的Servlet

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String abc = request.getParameter("students");
        System.out.println("JSON >  "+abc);
    }

我在控制台中获得输出:

JSON > null

2 个答案:

答案 0 :(得分:0)

我只需要将data调用的ajax元素更改为:

data: {students : JSON.stringify(SendInfo)},

答案 1 :(得分:-1)

request.getParameter()从URL读取数据(查询参数)或形成发布的参数(application / x-www-form-urlencoded,multipart / form-data content type)。

如果你发布JSON,你必须从servlet inputStream(request.getInputStream)中读取它,并解析字符串。