Pass variable to Javascript for Controller SpringMVC

0

I would like to pass one (or more) variable to the SpringMVC controller

  

login.html

   <form id="form-login" method="post">
        <div class="log-input">
           <div class="log-input-left">
              <input type="text" id="usuario" class="user"   onblur="buscar()" placeholder="Usu&aacute;rio"/>
           </div>
        </div>
        <div class="log-input">
          <div class="log-input-left">
              <input type="password" class="lock" value="password" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Email address:';}"/>
          </div>

        </div>
      <div class="log-input">
        <div class="log-input-left">
            <input type="text" class="empresa" value="Empresa" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Email address:';}"/>
        </div>
    </div>     
    <button class="btn btn-acessar">Acessar o Sistema</button>
</form>  
  

acaologin.js

function buscar(){
        alert('Salvar');
       var nome = document.getElementById("usuario").value;

    jQuery.ajax({
        type:  'POST',
        url:   'empresa',
        dataType: 'json',
        data: {
            usuario     : nome
        },
        success: function(data){
           alert(data.response);
        }
    });       
}
  

Controller

@RequestMapping ("/ company")

 public @ResponseBody String  empresa (@RequestParam(value = "usuario") String usuario){
       Usuario_Controller uc = new Usuario_Controller();
       JOptionPane.showMessageDialog(null, "Empresa");
       String empre = uc.recuperarEmpresa(usuario);       
       return "{\"response\" : \""+empre+"\"}";
    }

But when trying to look for information, analyzing the chrome debugger it shows this message:

Provisional headers are shown

    
asked by anonymous 06.04.2017 / 21:45

1 answer

1

I did it!

Use the method like this:

@RequestMapping("/empresa")
    public @ResponseBody String  empresa (@RequestParam(value = "usuario", required=false) String usuario){
       Usuario_Controller uc = new Usuario_Controller();
       //JOptionPane.showMessageDialog(null, "Empresa");
       String empre = uc.recuperarEmpresa(usuario);       
       return "{\"response\" : \""+empre+"\"}";
    }
    
07.04.2017 / 16:36