简体   繁体   中英

Redirecting after a AJAX form post

So I am currently investigating an issue a friend came to me with. I really don't have a solid understanding of either AJAX or jQuery . Near from what I can tell, when this form is posted, it is going to the current address/set_var.cgi with the index of 94 and is being redirected to currentaddress/scale405.html . He wants to be able to set the address in the second input to an actual address like www.google.com so the user is redirected to a different site after the form post. I've tried looking in the jQuery and AJAX scripts and I am at a total loss.

if someone would like to take a look, its www.gloryfeed.com .

<form name="requestPrint" action="http:set_var.cgi" method="POST">
  <input name="value" type="hidden" size="8" value="1"> 
  <input type="hidden" name="page" value="scale405.html">
  <input type="hidden" name="index" value="94">
</form>

So I guess my question is, is there a way to format the URL that is entered into that value so it redirects properly without having to change the AJAX and jQuery ?

The redirect itself is very easy:

doc.location = redirect_url;

The redirect_url would come from the AJAX data. It depends on whether you get JSON or XML as data. If set, then redirect.

With XML you can use jQuery to retrieve the data. Maybe something like that (untested):

redirect = jQuery("redirect", result.jqxhr.responseXML);

You may have to handle all sorts of situations, the primary one being errors. If the form you just sent has a missing field, you probably want to generate an error instead of a redirect. But the basics is pretty simple as shown above.

I wrote a server_access.js script that handles redirects, errors, sending data both ways... It's a plugin part of Snap! Websites .


As a side note, using a Location: redirect_url in the HTTP header won't help. The AJAX response won't ever trigger a redirect, which is by design.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM