Help with ajax connection

Asked

Viewed 19 times

2

Hello! I am new in php and am starting with ajax, I would like a help because I am locked in this project. I am passing this connection ajax and using the data Return to send the object to my other function. However my log console shows that I am sending a string and not an object as expected.

function carregarDados(arquivo, type, dados, metodo){

    $.ajax({
        url: '../controller/'+arquivo+'Controller.php?metodo='+metodo,
        type: type,
        dataType: 'json',
        data: dados,
        success: function(dados){

        }
    });

    return dados;

}

function logar(){

    var dados = $('#formLogin').serialize();
    var retorno = carregarDados('login', 'post', dados, 'logar');

    console.log(retorno);

}  

1 answer

0

Sends string as the method serialize jQuery returns a string. Use the method serializeArray to get an array of field names and values. For example the HTML form:

 <form onsubmit="logar()" id="formLogin">
    <input type="text" name="login" value="admin">
    <input type="text" name="senha"value="123">
    <input type="submit">
  </form>

With serializeArray:

[{
  name: "login",
  value: "admin"
}, {
  name: "senha",
  value: "123"
}]

Browser other questions tagged

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