Send Post Data on Redirect with JavaScript/Jquery

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!

Send POST data on redirect with Javascript/jQuery to a top level iframe?

Set target="_top" in the iframe's form tag. That tells the browser to load the response in the parent frame.

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.

How do I redirect to another webpage?

One does not simply redirect using jQuery

jQuery is not necessary, and window.location.replace(...) will best simulate an HTTP redirect.

window.location.replace(...) is better than using window.location.href, because replace() does not keep the originating page in the session history, meaning the user won't get stuck in a never-ending back-button fiasco.

If you want to simulate someone clicking on a link, use
location.href

If you want to simulate an HTTP redirect, use location.replace

For example:

// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");

// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";

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! ;)



Related Topics



Leave a reply



Submit