1

This is the client code:

function save(calc){
    var request = new XMLHttpRequest();

    request.open("POST","/Calculator/Controller?action=saveCalc",true);
    //request.send("calc=calc")   ---this does not work also
    request.send(calc);
}

this is the servlet:

if (action.equals("saveCalc")) {
        String parameter = request.getParameter("calc");
        System.out.println(parameter);
}

The parameter here is null...

I also tried like that:

function save(calc){
        var request = new XMLHttpRequest();
        request.open("POST","/Calculator/Controller?action=saveCalc",true);
        var data = new FormData();
        data.append('calc', 'calc');
        request.send(data);
    }

also does not work.

How to get this .... parameter. :D

I succseed. Here is the solution: - content-type ...

function save(calc){
    var request = new XMLHttpRequest();
    request.open("POST","/Calculator/Controller?action=saveCalc",true);
    request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    request.send("calc=" + calc);
}


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

        String action = request.getParameter("action");

        if (action.equals("saveCalc")) {
            String calc = request.getParameter("calc");
            System.out.println(calc);
        }
}
johnpolqkov
  • 93
  • 2
  • 13

2 Answers2

1

First you are doing wrong, since the parameter from GET travel by the URL, so if you are using POST and sending parameters by URL this should not be traveling in the request, POST means that the parameter travel by the request body so yo have to do something like:

var form = document.getElementById('subscribe_frm');
form.setAttribute("method", method);
form.setAttribute("action", path);
form.submit();

You could first create the attributes.

An alternative to get your code workin it is to:

request.open("GET","/Calculator/Controller?action=saveCalc",true);

Move POST to GET

Daniel Hernández
  • 4,078
  • 6
  • 27
  • 38
  • I want to post data to the server and then save it to the database. More logically is to use post than get don't u think? Also Even when I am using GET, again the parameter in the servlet is null ........ – johnpolqkov Jun 03 '15 at 16:24
  • Yes, it is better to use POST in that case, I did not know your intention. I think it is better you should post the remain code from your servlet, how are you getting 'action', are you using the doPost method, please, paste the code to help you in better way? – Daniel Hernández Jun 03 '15 at 17:19
0

Get the form data

  var parameter = $("input#CountryVal").val();
   dataString = "calc=" + parameter ;

Make Ajax call

 $.ajax({
                type: "POST",
                url: "/Calculator/Controller?action=saveCalc",
                data: dataString,
                dataType: "json",

                //if received a response from the server
                success: function( data, textStatus, jqXHR) {...}
Sheetal Mohan Sharma
  • 2,908
  • 1
  • 23
  • 24