1

I have a javascript object that i want to pass to a java Servlet, how can i perform this operation ? I have already tried few things but didnt work out.

Here is my code :

$.ajax({
    url: 'TestUrl',
    data: {
        object: myJavaScriptObject
    },
    type: 'GET'
});

and in the servlet (doGet method)

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String result = request.getParameter("object");
    System.out.print(result);
}

but i just get null in the console.

I'm also interested on how to perform the opposite operation, pass a java object in the servlet to a JavaScript Object.

Thank you in advance.

2
  • 1
    Try using post instead of get. Commented Aug 1, 2017 at 9:08
  • I dont think "object" will be a request parameter, but a query string. Commented Aug 1, 2017 at 9:10

2 Answers 2

1

Change GET to POST for sending data.

    $.ajax({
    url: 'TestUrl',
    dataType: 'json',
    data: {
        object: myJavaScriptObject
    },
    type: 'POST'
    });
Sign up to request clarification or add additional context in comments.

Comments

0
$.ajax({
                        url: 'TestUrl',
                        method: 'post',
                        data: {
                            object: myJavaScriptObject
                        },
                        success: function(data) {
                            if(data.success) {
                //process your data here
                }
                        }
                    });

List item

Comments

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.