javascript - Basic Login ajax-php, (XMLHttpRequest). Sometimes work, sometimes doesn't

one text

I'm just coming back to web developing. In my first steps, I'm working on a basic log in which is turning me crazy. Here we go:

  1. Js code just react to the login button, sending id and password.

  2. PHP code checks the data received on the ddbb and returns a code error if there is a mismatch, or a URL to redirect if the login was successfully.

  3. Js get the answer from php and then displays a message to the user or just redirects to the personal area.

Despite the rudimentary off the code and the design mistakes ( just trying to make it run), many times it works successfully, but many other it didn't get more than a 1 (server connection ok) on the onreadystatechange. So I need to ask, someone know what could it be?

Why onreadystatechange keeps it stuck on first step sometimes ? What am I missing?

Thanks a lot.

(once I get this one working, I will try again with other tools, minding jQuery instead basic js and jwt instead sessions, but this will be next one. I'll accept suggestions )

JavaScript code:

var boton = document.getElementById("boton");
boton.addEventListener('click', validarLogIn);

function validarLogIn()
{
    var nombre = document.getElementById("nombre");
    var contrase?�a = document.getElementById("contrase?�a");
    var parametros="Nombre="+nombre.value+"&Contrase?�a="+contrase?�a.value;
    http_request = new XMLHttpRequest();
    http_request.onreadystatechange = resultadoLogIn;
    http_request.open("POST", "../php/validarLogin.php");
    http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http_request.send(parametros);
}

function resultadoLogIn()
{
    var respuesta=http_request.responseText;
    if (http_request.readyState == 4){
        if (http_request.status == 200){
            if(respuesta==1){
                    alert("Contrase?�a incorrecta.");
                }else if(respuesta==2){
                        alert("Usuario no encontrado.");
                    }else{
                        window.location=respuesta; 
                    }
            }else{
                alert('Hubo problemas con la petici??n.');
            }
        }
}

PHP code

$nombre =$_POST['Nombre'];
$contrase?�a =$_POST ['Contrase?�a'];
require_once 'DatosBBDD.php';
$consultaBbdd="SELECT ID,CONTRASE?�A FROM usuarios WHERE ID ='".$nombre."'";
$urlRedireccion="../php/vistaGeneral.php";

$link = mysqli_connect($ip,$usuarioBbdd,$contrasenaBbdd);
mysqli_select_db($link, $nombreBbdd);
$result = mysqli_query($link,$consultaBbdd);
mysqli_data_seek ($result, 0);
$extraido= mysqli_fetch_array($result);
mysqli_close($link);

$nombreV=false;
$contrase?�aV=false;

if(isset($extraido['ID'])&&isset($extraido['CONTRASE?�A'])){ 
    if ($extraido['ID']==$nombre){$nombreV=true;}
    if ($extraido['CONTRASE?�A']==$contrase?�a){$contrase?�aV=true;}
}

if($nombreV){
    if($contrase?�aV){
        session_start();
        $_SESSION['idUsuario']=$nombre;
        echo $urlRedireccion;
    }else{echo "1";}
}else{echo "2";}

Source