Send POST data on redirect with JavaScript/jQuery?
Construct and fill out a hidden method=POST action="http://example.com/vote"
form and submit it, rather than using window.location
at all.
jQuery - Redirect with post data
There is a JQuery plug-in that accomplishes pretty much what you're trying to do: https://github.com/mgalante/jquery.redirect/blob/master/jquery.redirect.js.
After including JQuery and the jquery.redirect.min.js plug-in, you can simply do something like this:
$().redirect('demo.php', {'arg1': 'value1', 'arg2': 'value2'});
Use the following code on newer JQuery versions instead:
$.redirect('demo.php', {'arg1': 'value1', 'arg2': 'value2'});
Hope this helps!
Redirect after POST request with jquery
Ok, I've found the magical solution! :)
The situation:
there is a view's method called by jQUery.ajax()
To redirect after Ajax, I've used the tips found here (use document instead of 'body' in the jQuery selecotor)
But, I've had also further operations to do.
in the called view, call another method, like this:
[...]
if request.method == 'POST':
if "obj" in request.POST: #ricevuti dati da ajax
request.session['qual'] = request.POST['obj']
return HttpResponseRedirect(reverse("xenopatients.views.qualReport"))
I save the data that I need in the session. I use this data in the called view.
Call another method it's the key for redirect the page of the browser.
In fact, at the end of the called method, you can normally write&execute:
return render_to_response('measure/qualReport.html', {'name':name, 'list_report': report, 'message':'Measures correctly saved'}, RequestContext(request))
Hope this can be useful for someone! ;)
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.
Related Topics
Expose Jquery to Real Window Object with Webpack
Export JavaScript Data to CSV File Without Server Interaction
Foreach on Array of Undefined Created by Array Constructor
Differencebetween the Different Scroll Options
How to Clone HTML Element Objects in JavaScript
Is This Simple String Considered Valid JSON
Url Encode a String in Jquery for an Ajax Request
Reactjs Async Rendering of Components
Highlight Text Range Using JavaScript
How to Maintain State After a Page Refresh in React.Js
Good Ways to Improve Jquery Selector Performance
Password Encryption at Client Side
Testing Whether a Value Is Odd or Even
How to Delete Document from Firestore Using Where Clause
How to Find JavaScript Variable by Its Name
Difference Between "Module.Exports" and "Exports" in the Commonjs Module System
How to Open Specific Tab of Bootstrap Nav Tabs on Click of a Particuler Link Using Jquery