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 ..... } 

    1 answer 1

    And what if it is (fixed the selector and set the full URL):

     <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(); var data = $("#data").val(); //$.post('add_task', {data : data}, function(result){ $.post('http://test/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> 
    • Yes, the problem is in the selector, thanks, but the full path is not necessary; my folder is protected from direct access only through the servlet running. - Pavel