Boa tarde.
Quando envio meu formulario via POST, ele não está atribuindo os valores dos campos para que eu possa comparar no lado servidor, e ele me retorna undefined, alguma luz? Segue o código:
function foundUser(){
$.ajax({
type: "POST",
url: "rest/authenticationRest/searchUser",
data: JSON.stringify($("#authentication")),
contentType: "application/json",
success:function(date){
alert(date.login)
},error(err){
console.log(err);
alert("Erro ao processar a requisição " + err.responseText);
}
});
}
package br.com.festivalRest.jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import br.com.festivalRest.rest.jdbcinterface.FestivalDAO;
import br.com.festivalRest.objetos.User;
public class JDBCAuthenticationDAO implements FestivalDAO {
private Connection conexao;
public JDBCAuthenticationDAO(Connection conexao){
this.conexao = conexao;
}
public boolean searchUser(User user){
boolean checkUser = false;
String selectUser = "select * from usuarios where login = ? and senha = ?";
PreparedStatement p;
try{
p = this.conexao.prepareStatement(selectUser);
p.setString(1, user.getLogin());
p.setString(2, user.getSenha());
ResultSet rs = p.executeQuery();
while(rs.next()){
System.out.println(user);
checkUser = true;
}
} catch(Exception e){
e.printStackTrace();
}
return checkUser;
}
}
package br.com.festivalRest.rest.authentication;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.core.Response;
import com.google.gson.Gson;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import br.com.festivalRest.bd.conexao.Conexao;
import br.com.festivalRest.jdbc.JDBCAuthenticationDAO;
import br.com.festivalRest.objetos.User;
@Path("authenticationRest")
public class AuthenticationRest extends UtilRest {
public AuthenticationRest(){
}
@POST
@Path("/searchUser")
@Consumes(MediaType.APPLICATION_JSON)
public Response foundUser(String usuarioParam){
try{
Conexao conec = new Conexao();
Connection conexao = conec.abrirConexao();
JDBCAuthenticationDAO jdbcAuthentication = new JDBCAuthenticationDAO(conexao);
Gson gson = new Gson();
User user = gson.fromJson(usuarioParam, User.class);
jdbcAuthentication.searchUser(user);
conec.fecharConexao();
return this.buildResponse("OK");
}catch(Exception e){
e.printStackTrace();
return this.buildErrorResponse("Falha");
}
}
}
<div class="modal fade" id="modal-authentication" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Faça seu login</h4>
</div>
<div class="modal-body">
<form id="authentication" class="form">
<div class="form-group">
<label for="usuario">Usuario</label>
<input type="text" class="form-control" id="login" name="login" placeholder="Informe seu usuario" >
</div>
<div class="form-group">
<label for="password">Senha</label>
<input type="password" class="form-control" id="senha" name="senha" placeholder="********" >
</div>
</form>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal" onclick="foundUser()">Enviar</button>
</div>
</div>
</div>
</div>
</div>