1

Here is my AJAX code which triggers the Servlet 'CalculateLace'

laceTd.dblclick(function() {
    var jsonObj= { jsonObj: [ {"rowID": $(nRow).attr('id')}  ]};                                    
    $.ajax({
        data: JSON.stringify(jsonObj),
        contentType: "application/json; charset=utf-8",
        traditional: true,
        url: "CalculateLace"
    });
});

And here is my Java Servlet code:

protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
      String id = req.getParameter("rowId");
      //do something
}

But I keep getting String id as null. I also tried

String id = req.getParameter("id");

but to no avail. What am I doing wrong here?

5
  • and what this do - req.getParameter("jsonObj"); Commented Apr 16, 2013 at 20:08
  • 1
    You are passing one parameter, jsonObj, an array with one object inside with one property "rowID". Commented Apr 16, 2013 at 20:09
  • 3
    Where are you decoding the JSON on the server side? Commented Apr 16, 2013 at 20:09
  • I have been using this example: stackoverflow.com/questions/15837842/… Commented Apr 16, 2013 at 20:11
  • As Mike mentioned you need to decode the JSON on the servlet, in order to be able to read the attributes such as rowId . Commented Apr 16, 2013 at 20:14

1 Answer 1

2

Try this way -

var jsonObj= {"rowId": $(nRow).attr('id')};

and get rowID in your servlet this way - You can get library to parse your json here JSON.org

req.getParameter("rowId");
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. This also worked for me: data: "rowID=" + $(nRow).attr('id'),

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.