Ajax function does not pass the Success

Asked

Viewed 393 times

7

My function calling my web service

  var email = document.getElementById("email").value  
     var senha = document.getElementById("senha").value 

      $.ajax({
        url: "http://localhost:8080/Servidor/rest/service/loginCustmerUser/"+ email + ","+ senha,
        dataType: "json",
        type: "GET",
        async: false,
        success: function (data) {
           alert(data)
        }, 
        error: function(e){
            alert("Erro: " + e);

        }, 

    }); 

My method in the webservice

@GET
@Produces("application/json")
@Path("/loginCustmerUser/{email},{senha}")
public String loginClientUser(@PathParam("email") String email,
        @PathParam("senha") String senha) {
    NotaFiscalBO bo = new NotaFiscalBO();
    CustmerUser cUser = bo.loginCustmerUser(email);


    try {
        boolean validacao = validarCustmerUserLogin(senha,
                cUser.getPassword());
        if (!validacao) {
            cUser = null;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return new Gson().toJson(cUser);
}

Return of the method

{"id":3,"login":"test","password":"2482e34cc83b09ba9088b2af8bf11866"}

  • 1

    NAY use the GET method for this type of request!

  • Try to return the object cUser normally without converting to JSON as seen in http://stackoverflow.com/questions/13594945/how-correctly-produce-json-by-restful-web-service

  • Where and how are you using this ajax call? Is it in form Submit? It can show the html code involved?

  • Rafael did it the way you said but not right, even as POST or returning my object goes straight to error. Dherik I’m calling on a button with onclick.

  • Rafael returned the object, in the browser it returns ok, but in ajax it goes straight to error

1 answer

1

Apparently there is nothing wrong with your code, try running the URL in a browser and check whether it will return the logged in user. Normally I do so:

@GET
@Path("/loginCustmerUser")
@Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8")
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
public Response logar(@QueryParam("email") String email,
        @QueryParam("senha") String senha) {

        LoginService loginService = new LoginService();
        Usuario usuario= loginService.logar(email, senha);
        return Response.status(Response.Status.ACCEPTED).entity(usuario).build();

}   

My URL would look like this: http://localhost:8080/Server/Rest/service/loginCustmerUser? email=Password=TEST

I hope I’ve helped.

  • Dude I did like you said, I’m taking the following error - Messagebodywriter not found for media type=application/json; charset=utf-8, type=class br.com.servidor.entity.Custmeruser, genericType=class br.com.servidor.entity.Custmeruser.

  • I corrected this error with lib Genson putting the return like this - Genson(). serialize(cUser); now my browser return is {"client":null,"id":3,"login":"test","password":"2482e34cc83b09ba9088b2af8bf11866"}. But it still goes straight to error

Browser other questions tagged

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