Pass variable to Javascript for Springmvc Controller

Asked

Viewed 765 times

0

I would like to pass a (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 get information, analyzing Chrome’s Debugger it shows this message:

Provisional headers are shown

inserir a descrição da imagem aqui

1 answer

1

I got!

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+"\"}";
    }

Browser other questions tagged

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