troubleshooting Question

Sending objects with objects to java servlet using ajax

Avatar of MWilliamsBolton
MWilliamsBolton asked on
JavaAJAXjQuery
5 Comments1 Solution2279 ViewsLast Modified:
I am trying to send an object from a browser to a java servlet and store it in a JSON object on the server.

I have the following code on the browser

   $('#btnSubmit').click(function(){
         var newDetails = {age: 12, name:{firstname:"Martin", lastname:"Williams"}};

         $.ajax({
               type: "POST",
               url: "TestServlet",
               data: newDetails
         });

And the following code on the test servlet.

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            System.out.println("Age " + request.getParameter("age"));
            System.out.println("Name: " + request.getParameter("name"));

      }

The code returns the age for the simple entry but prints null for the name.

Is there a generic way to send a complex (ish) object in javasvript and put it into a JSON object on the server?

Thanks in advance

Martin.
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros