Reputation: 19
I need your help on how to get redirected to other html file on success ajax response in the javascript.
Below is my code where i am getting the successful ajax response in jsonvariable.
I have tried using window.location but its not working. is there any other way to do the same, Please let me know
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)
}
});
};
Upvotes: 1
Views: 1192
Reputation: 150
success: function (response) {
if(jsonvariable=='true'){
alert("redirect page to");
window.location = "http://www.google.com/";
}
},
failure: function (response) {
alert(response.d);
}
Any valid Ajax request will return either a success or failure. As per above issue, on success we need to redirect to another page or url. Window.location function help to redirect to another page or url.
Upvotes: 1
Reputation: 311
Since you're probably working in your domain (localhost in your case), you need to only add the file name you are linking to.window.location.href = 'homepage.html';
Resolve your file path based on your folder structure. See help https://en.wikipedia.org/wiki/Path_(computing)
Upvotes: 1
Reputation: 9
Use this function in after success:
$(document).ready(function() {
window.location.href = "http://www.google.com"";
});
Upvotes: 1
Reputation: 214
you can window.location.href = 'http://www.google.com'; or window.location = 'http://google.com';
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'){
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)
}
});
};
Upvotes: 1