How to manage a redirect request after a jQuery Ajax call
The solution that was eventually implemented was to use a wrapper for the callback function of the Ajax call and in this wrapper check for the existence of a specific element on the returned HTML chunk. If the element was found then the wrapper executed a redirection. If not, the wrapper forwarded the call to the actual callback function.
For example, our wrapper function was something like:
function cbWrapper(data, funct){
if($("#myForm", data).length > 0)
top.location.href="login.htm";//redirection
else
funct(data);
}
Then, when making the Ajax call we used something like:
$.post("myAjaxHandler",
{
param1: foo,
param2: bar
},
function(data){
cbWrapper(data, myActualCB);
},
"html"
);
This worked for us because all Ajax calls always returned HTML inside a DIV element that we use to replace a piece of the page. Also, we only needed to redirect to the login page.
res.redirect from an AJAX call
I don't think what you want to do is possible. An AJAX request is meant just to pass data back and forth. What happens now is that you need to script client side behavior on your end. This means the AJAX request will pass a 302 and other data that comes along for the ride to the callback on JS. No client side behavior can be altered from the server. It is up to you to do something with the AJAX returned values. If 500, throw an error message, 200 do something etc.
The only way you can get a server redirect to work is by traditional HTML form submission.
Redirecting after Ajax post
success: function(response){
window.location.href = response.redirect;
}
Hope the above will help because I had the same problem
Unable to redirect after jQuery Ajax call
What is the output of console.log(data)
?
It seems the server is redirecting so when the front end code recieves the output from the server, it is already the either the successRedirect or failureRedirect page, so your code window.location.href = data.redirect
is not reached. Check data
, it should have the redirect property. If not, my guess is that the server is already doing the redirect and you are not seeing it because you are calling it via ajax. This can be confirmed in the network tab of the web inspector.
Page redirect with successful Ajax request
Sure. Just put something at the the end of your success function like:
if(result === "no_errors") location.href = "http://www.example.com/ThankYou.html"
where your server returns the response no_errors
when there are no errors present.
Ajax not redirecting after a successful response from php
You have to trim your response....sometimes the string recieved from the server contains whitespace.....and that causes the problem to not redirect to other page.
$.ajax({
url: 'actionlog.php',
method: 'POST',
data: $('#login-form').serialize() + '&action=login',
success: function(response) {
if ($.trim(response) === 'true') {
window.location = '../';
} else {
$('#logAlert').html(response);
}
}
});
Related Topics
Why Does Calling React Setstate Method Not Mutate the State Immediately
Onclick or Inline Script Isn't Working in Extension
How to Create a Two Dimensional Array in JavaScript
How to Append Something to an Array
Events Triggered by Dynamically Generated Element Are Not Captured by Event Handler
Getting the Id of the Element That Fired an Event
Jquery .Live() VS .On() Method For Adding a Click Event After Loading Dynamic Html
How to Convert a String to Boolean in JavaScript
How to Get a Key in a JavaScript Object by Its Value
How to Reverse a String In-Place in JavaScript
How to Obfuscate (Protect) JavaScript
How to Get Client'S Ip Address Using JavaScript
How to Disable Scrolling Temporarily
How to Send a Cross-Domain Post Request Via JavaScript
Get the Highlighted/Selected Text
Disable/Enable an Input With Jquery
What Does [Object Object] Mean
What's the Difference Between "Array()" and "[]" While Declaring a JavaScript Array