0

I'm going to use jQuery Ajax to post requests:

var xhr = $.ajax({
               url: "/UsersCounter", 
               type: "POST", 
               cache: false, 
               data: {"emailAddr": "example@domain.com"}, 
               dataType: "text", 
               contentType: "application/json"
         });

and server side I have a simple servlet to handle http requests:

  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

   String x=req.getParameter("emailAddr");
   ...
  }

In browsers parameter of emailAddr is visible in POST requests but value of x is null in the servlet. Is there anything wrong with this code?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Super Hornet
  • 2,839
  • 5
  • 27
  • 55

1 Answers1

-2

Every thing fine except this line

  data: {"emailAddr": "example@domain.com"},

"emailAddr" should not be in quotes, instead above line use following syntax

 data: {emailAddr: "example@domain.com"},

I think Now this will work fine

beside this you can use datatype application/json then in data you can use json.stringify to avoid simple mistakes before calling asynchronous request to server.