I have a simple ajax request that should send input to the server through a servlet without reloading the page. But the server is not receiving data.
Please explain why it does not work and how to fix it?
This is my village:
<head> <title>home</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#send_task").click(function(){ var data = $("select#data").val(); $.post('add_task', {data : data}, function(result){ $("#answer_from_server").html(result); }); }) }); </script> </head> <body> <label for="data">Enter the task</label><input id="data" type="text"> <input id="send_task" type="button" value="Ok" /> <p id="answer_from_server"></p> </body> And servlet:
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println(req.getParameter("data")); // всегда печатает null ..... }