3

I'm trying to make a simple ajax request using Java (JSP + Servlet) and Ajax (jQuery). The Ajax request is working as expected, and the servlet code is reached.

The problem is that I can't get the values of the parameters sent by the request. I get null values.

This is the code in the servlet:

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

    response.setContentType("application/json");

    String perfilId = request.getParameter("perfilId"); //Null value
    String perfilNombre = request.getParameter("perfilNombre");  //Null value


    try (PrintWriter out = response.getWriter()) {
        Gson gson = new Gson(); 
        JsonObject obj = new JsonObject();
        obj.addProperty("mensaje", "Algún mensaje. Id: " + perfilId + ", Nombre: " + perfilNombre);

        out.print(gson.toJson(obj));
        out.flush();
    }
}

Ajax request inside a JSP:

$.ajax({
    type: "POST",
    url: 'srvl_def',
    cache: false,
    contentType: "application/json;",
    dataType: "json",
    data: {
        perfilId: $('#perfilId').val(),
        perfilNombre: $('#perfilNombre').val()
    }, 
    success: function (data) {
        alert(data.mensaje);

    }
});

The request data looks like this:

perfilId=1&perfilNombre=nuevo

Perhaps I'm missing something?

EDIT

This is the HTML

    <input type="text" id="perfilId" />
    <input type="text" id="perfilNombre" />

    <button type="button" id="btnGuardar">Enviar</button>

    <script src="js/jquery.js" type="text/javascript"></script>
    <script type="text/javascript">
        $('#btnGuardar').click(function (){
            //ajax call
        });
    </script>
6
  • 1
    Try passing it as 'perfilId': $('#perfilId').val(). Adding quotes. Or use JSON.stringify(..) Commented Apr 9, 2015 at 17:30
  • @ShaunakD I had already tried using JSON.Stringify, and just tried using quotes. I still get null values. Commented Apr 9, 2015 at 17:40
  • See the asnwers here stackoverflow.com/a/19376331/3639582 Commented Apr 9, 2015 at 17:45
  • Paste the HTML please... Commented Apr 9, 2015 at 17:50
  • @DarkHorse HTML pasted. Commented Apr 9, 2015 at 17:58

2 Answers 2

3

Following this answer, @ShaunakD referenced this in a comment (see question), I was able to obtain the values sent by the ajax call.

The call looks like this:

var perfilId = $('#perfilId').val();
var perfilNombre =  $('#perfilNombre').val();

$.ajax({
    type: "POST",
    url: 'srvl_def',
    cache: false,
    contentType: "application/x-www-form-urlencoded; charset=UTF-8;",
    dataType: "json",
    data: {
        perfilId: perfilId,
        perfilNombre: perfilNombre
    },
    success: function (data) {
        alert(data.mensaje);
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

I encountered the same issue when making a POST request and found that removing the contentType parameter allowed the non-empty request parameters to be correctly passed and read by a servlet.
0

if your ajax call is inside some function try this :

var perfilId = $('#perfilId').val();
var perfilNombre =  $('#perfilNombre').val();
$.ajax({
    type: "POST",
    url: 'srvl_def',
    cache: false,
    contentType: "application/json;",
    dataType: "json",
    data: {
        perfilId: perfilId ,
        perfilNombre: perfilNombre
    }, 
    success: function (data) {
        alert(data.mensaje);

    }
});

2 Comments

I just tested this and I still get null values.
Can you paste the HTML... It will help to resolve :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.