我需要你的帮助,了解如何在 javascript 中成功 ajax 响应时重定向到其他 html 文件。
下面是我在 jsonvariable 中获得成功的 ajax 响应的代码。
我试过使用 window.location 但它不起作用。有没有其他方法可以做到这一点,请让我知道
function myFunction() {
$.ajax({
url: '/ValidateOTP',
type: 'POST',
data:JSON.stringify( $('#OTP').val()),
contentType: 'application/json;charset=UTF-8',
success: function(response){
jsonvariable=response['success'].toString();
if(jsonvariable=='true')
alert('done');
else if(jsonvariable=='false')
document.getElementById("div2").innerHTML="OTP didn't match !! Please click the GET OTP button to re-generate OTP";
document.getElementById("div2").style.color="Red";
},
error: function(response){
alert(response)
}
});
};
最佳答案
你可以 window.location.href = ' http://www.google.com ';或者 window.location = ' http://google.com ';
函数 myFunction() {
$.ajax({
url: '/ValidateOTP',
type: 'POST',
data:JSON.stringify( $('#OTP').val()),
contentType: 'application/json;charset=UTF-8',
success: function(response){
jsonvariable=response['success'].toString();
if(jsonvariable=='true'){
window.location.href = 'http://www.google.com';
}
else if(jsonvariable=='false')
document.getElementById("div2").innerHTML="OTP didn't match !! Please click the GET OTP button to re-generate OTP";
document.getElementById("div2").style.color="Red";
},
error: function(response){
alert(response)
}
});
};
关于javascript - 如何在成功ajax响应时重定向到其他html文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58463091/