Kunal Vashist
Kunal Vashist

Reputation: 2471

Not able to read the JQuery data sent from Ajax in Java Servlet

Here is my Ajax code:

var email="[email protected]";    

    $.ajax({
        url : "ships",
        data : "{email" + email.toString() + "}",
        success : function(data){
            alert(data)
        },
        error : function(data) {
            console.log("error:", data);
        },

        type : "post"
    });

And here is my Java Servlet code:

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

    System.out.println(request.getParameter("email"));


}

I CANNOT read data in Java Servlet The console prints out the following value for email:

null

I am using Tomcat 7

Can anyone please tell me what i am doing wrong and why i cannot read data in Java Servlet_

Upvotes: 1

Views: 2033

Answers (1)

Gustav Barkefors
Gustav Barkefors

Reputation: 5086

The property data of the param object is a JavaScript object, so to send a parameter named EmailAddress you would do:

...
url : "ships",
data : {
    EmailAddress: email.toString()
},
success : function(data){
    ...

Upvotes: 2

Related Questions