0

I am trying to read a json object that I sent from my ajax to servlet but then I keep receiving null as the value. I tried to log the json object that I am sending from my ajax and it looks fine to me. Can someone tell me what could be my error?

$(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");

}

The json object that I am sending

{
    "apiname": "jdjdj",
    "apiendpoint": "sdjsdj",
    "apiversion": "djdjd",
    "source": "internet"
}
Rachel
  • 173
  • 4
  • 12

0 Answers0