简体   繁体   中英

Window.location doesn't work after ajax request

Window.location doesn't work after a success respond when it call a ajax request, the success function it called.

when i execute on firefox on debugger step by step the window.location it works .

function login(){

var jason ={"usuario":document.getElementById("inputusuario").value,
        "password": document.getElementById("inputpassword").value
           };
            json =JSON.stringify(jason);

          console.log(json);

var onSuccess = function (data) {
    console.log('Success');
    window.location ='salas.html'

};
var onError = function (jqXHR, textStatus, errorThrown) {
    console.log(jqXHR);
    console.log(textStatus);
    console.log(errorThrown);
    alert("no se conecto");

};

var onBeforeSend = function () {
    console.log("Loading");
};

var jason ={"usuario":document.getElementById("inputusuario").value,
        "password": document.getElementById("inputpassword").value
           };

            json =JSON.stringify(jason);
$.ajax({

    url: "../reservaciones/index.php/login",
    type:"POST",
     data: json,
    cache: false,
    async: false,
    beforeSend: onBeforeSend,
    error: onError,
    success: onSuccess
 });
};

I copied your code exactly, added the appropriate elements, and it worked just fine.

This is your code, as is: 的jsfiddle

Try these:

1. Try window.location.assign() . This has been known to fix this very problem.

2. Try running async: true . From your code I see no reason why you'd want to run false and sometimes that can cause timing issues.

You're missing the trailing ';' in

window.location ='salas.html'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM