无法在servlet中获取Json对象?

时间:2017-07-24 07:42:10

标签: java jquery json ajax servlets

我正在尝试读取我从ajax发送到servlet的json对象,但后来我继续接收null作为值。我试图记录我从ajax发送的json对象,它看起来很好。有人能告诉我可能是我的错误吗?

$(document).on("click", "#check", function() { // When HTML DOM "click" event is invoked on element with ID "somebutton", execute the following function...
      event.preventDefault();
      var data = $("#register").serialize().split("&");
      var obj={};
        for(var key in data)
        {
            console.log(data[key]);
            obj[data[key].split("=")[0]] = data[key].split("=")[1];
        }
        console.log(obj);
     $.ajax({
            type: "POST",
            url: "HomeServlet",
            dataType:'json',
            data:JSON.stringify(obj),
           success: function(data){
                console.log(data);
            },
            error:function(){
                console.log("error");
            },

        });
});

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("application/json");       
        PrintWriter out= response.getWriter();
        String data = request.getParameter("obj");//this is what I get as null
        String n = request.getParameter("apiname");
        String p = request.getParameter("apiendpoint");
        String e = request.getParameter("apiversion");

}

我发送的json对象

{
    "apiname": "jdjdj",
    "apiendpoint": "sdjsdj",
    "apiversion": "djdjd",
    "source": "internet"
}

0 个答案:

没有答案