Ajax does not work in Firefox

1

It works perfectly on the Chrome browser, and other browsers do not ...

When I type the login and password it displays the message "Login Done Successfully!" and the page is stopped in Mozilla.

See the code:

echo "<meta http-equiv=\"refresh\" content=\"1;URL=inicio.php\" />";
echo "Login Efetuado com Sucesso!";

$(function() {
    caminho = $('.logar');
    action = 'ajax/php/logar.php';
    enviar = $('form[name="logar"]');

    function resposta(datas) {
        caminho.html(datas);
    }

    enviar.submit(function() {
        var cadastrar = $.post(action, $(this).serialize());
        cadastrar.progress(resposta('<center><i class="icon-spin1 animate-spin"></i>Carregando...</center>'));
        cadastrar.done(resposta);
        cadastrar.fail(function() {
            resposta('Erro ao Cadastrar');
        });
        return false;
    });
});

What am I doing wrong?

    
asked by anonymous 19.02.2015 / 07:38

1 answer

2

The problem is not that the request is not working, it's the way you're trying to redirect the user. Chrome is probably the only one that considers the metatag you're using after page loading.

As a workaround, I suggest you change your response code for something like this:

echo "<script>
         setTimeout(function() {
             window.location.href = 'inicio.php';
         }, 1000);
     </script>";

echo "Login Efetuado com Sucesso!";

But still, I do not like this way of putting javascript on the server. Although I like each one, I recommend you change the response of the server, for example, return a JSON object and treat the response in javascript. Here's an example:

// php
$resposta = array();
$resposta["loginEfetuado"] = true;
echo json_encode($resposta);

// js
function resposta(data) {
    if (data.loginEfetuado) {
        caminho.html("Login Efetuado com Sucesso!");
        setTimeout(function() {
            window.location.href = "inicio.php";
        }, 1000);
    } else {
        caminho.html("Falha ao tentar efetuar login!");
    }
}
    
19.02.2015 / 11:58