Ajax Request Returning in White

Asked

Viewed 99 times

0

all right ?

My doubt is as follows: I am making an ajax request to my Serrvlet and its return is being blank.

JS:

$('#pesquisar').click(function() {
    $.ajax({
        url: '../UsuarioController',
        type: 'POST',
        data: {nome_pesquisa: $('#nome_pesquisa').val()},
        success : function(data) {
            console.log(data);
        },
        error: function(){
            console.log("erro");
        }
    });
    return false;
});

Servlet:

if(request.getParameter("nome_pesquisa").equals("") || request.getParameter("nome_pesquisa") != null){
        try {
            List<Usuario> usuarios = this.dao.search(request.getParameter("nome_pesquisa"));
            String json = new Gson().toJson(usuarios);

            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(json);
        } catch (Exception ex) {
            Logger.getLogger(UsuarioController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

JSP:

<form class="col-md-10">
                <div class="input-group">
                    <input class="form-control border-secondary py-2" type="search" id="nome_pesquisa" name="nome_pesquisa" placeholder="Pesquisar...">
                    <div class="input-group-append">
                        <button class="btn btn-outline-secondary" type="submit" id="pesquisar" name="pesquisar">
                            <i class="fa fa-search"></i>
                        </button>
                    </div>
                </div>
            </form>

Return obtained:

Retorno obtido

  • 1

    Based on these three snippets provided: could be anything. Tried to use print instead of write?

  • I tried, but it doesn’t print anything on the console :/

1 answer

1

Maybe it’s because you forgot to deny the condition

 !request.getParameter("nome_pesquisa").equals("")
  • 1

    Also, you must first do null validation, then use the "equals". Otherwise it may occur NullPointerException.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.