Windows位置不要在XMLHttpRequest之后重定向

Windows Location don't redirect after XMLHttpRequest

本文关键字:XMLHttpRequest 之后 重定向 位置 Windows      更新时间:2023-09-26

我有这些代码与XMLHttpRequest:

function Registrarse()
{
    conexionRegistrarse = new XMLHttpRequest();
    conexionRegistrarse.onreadystatechange = procesarRegistro;
    conexionRegistrarse.open('GET', 'index.php?registrarse=&username='+username+'&mail='+mail+'&pw='+contraseña+'&pwr='+repetircontraseña, true);
    conexionRegistrarse.send();
}
function procesarRegistro()
{
    var detalles = document.getElementById("labelUsername");
    if(conexionRegistrarse.readyState == 4)
    {
        if((conexionRegistrarse.responseText).indexOf("cuenta") == -1)
        {
            window.location = "http://localhost/index.php?creada";
        }
        else
        {
            detalles.innerHTML = conexionRegistrarse.responseText;
        }
    }
    else
    {
        detalles.innerHTML = "Cargando...";
    }
}

问题是,当一个成功的注册发生(这些发生在xmlhttprequest的responseText没有字符串"cuenta"),不要重定向到我:"index.php?creada"。

我测试过了。效果很好。代码:

var oAjax = new XMLHttpRequest();
oAjax.open('GET', 'a', true);
oAjax.send();
oAjax.onload = function() {
  if (oAjax.status == 200) {
    window.location = 'http://baidu.com';
  }
};

还有

,检查你的错误信息!