How to get Result from Controller Vraptor with Jquery?

Asked

Viewed 366 times

0

I have a controller.

Newslettercontroller.java

@Controller
public class NewsletterController{

       @Inject
       private Result result;

       @Post("/gravarEmail")
       public void gravarEmail(String email){

              String mensagemErro = "";
              CadastroEmail cadEmail = new CadastroEmail();
              cadEmail.setEmail(email);
              try{
                   cadEmail.gravar();
              }catch(Exception e){

                   e.printStackTrace();
                   mensagemErro = "Não foi possivel gravar o email";                  

              }

              result.include("mensagemErro",mensagemErro);

       }

}

And I have the code

$('#btnEnviar').click(function() {

  var email = $('email').val();

  $.post("/gravarEmail", {

    email: email

  }, function(status) {


    //Aqui é onde quero pegar o atributo do Controller o mensagemErro;


  });


});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>

<div id="divEmail">

  Insira o email:
  <br/>
  <input type="text" name="email" id="email" />

</div>

<div id="divBtnGravar">

  <input type="button" name="Enviar" id="btnEnviar" value="Enviar" />

</div>

What I intend to do is just take the contents of mensagemErro set in Controller and inserted in result. Remembering that if you see 2 similar emails, you will have an error in the bank mensagemErro

How to do this?

  • tried to give a console.log(status) to see the object it returns?

  • @Emirmarques , the console.log(status) returns 'Success', added date in Function thus function(data,status) and the data returns empty, but nothing I can catch the mensagemErro which is completed in the Controller

  • 1

    Try the following result.use(json()). from(messageErro). serialize();

No answers

Browser other questions tagged

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