javascript 使用ajax提交表单但重定向到另一个页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8768470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 04:31:01  来源:igfitidea点击:

Submit form with ajax but redirect to another page

javascriptjqueryjquery-plugins

提问by Lucy Weatherford

What should I use in the html and in the jquery if I want upon form submit to (a) execute a certain php file (url-a); (b) redirect to another one.(url-b)

如果我想在表单提交到 (a) 执行某个 php 文件 (url-a) 时,我应该在 html 和 jquery 中使用什么;(b) 重定向到另一个。(url-b)

I currently implemented the jquery form plugin. http://jquery.malsup.com/form/#getting-started

我目前实现了 jquery 表单插件。 http://jquery.malsup.com/form/#getting-started

Like so:

像这样:

step a. included jquery and form plugin scripts;

步骤 a. 包括 jquery 和表单插件脚本;

step b. in html, in the form element:

步骤 b。在 html 中,在表单元素中:

      <form action="url-a.php">
          <!--form code inserted here including input elements, etc and submit-->
      </form>

step c. in js, inside document ready:

步骤 c。在js中,里面document ready

      $('#form').ajaxForm(function() { 
           $.post("url-a.php", {a: a, b: b});
      }); 

Should one of these url links be changed? assume that url-a is the one the ajax should take care of, and I want to redirect to url-b..

是否应该更改这些 url 链接之一?假设 url-a 是 ajax 应该处理的那个,我想重定向到 url-b ..

Thank you!

谢谢!

回答by roselan

$('#form').ajaxForm(function() { 
  $.post("url-a.php", {a: a, b: b});
  window.location.href="/url-b.php";
});

回答by Rafay

if you want the page to redirect then why do you want it to submit ajaxlyany how in the success handler you can redirect to the page

如果您希望页面重定向,那么您为什么要它提交ajaxly任何如何在成功处理程序中重定向到该页面的内容

 $.post("url-a.php", {a: a, b: b},function(){
   window.location.href="/page/to/redirect.php";
 });

回答by Bozho

in the "complete" function of $.post(..)you should change the location of the browser. You can't use a server-side redirect with ajax.

在“完成”功能中,$.post(..)您应该更改浏览器的位置。您不能在 ajax 中使用服务器端重定向。

 $.post("url-a.php", {a: a, b: b},function(){
      window.location.href="/url-b.php";
 });

If you want to redirect before the ajax has completed, that's possible, but hard. I don't know how to do it in PHP, for example. The thing is, if you redirect immediately (again should be done via javascript), then the request is aborted from the client. And if the request is aborted, the server aborts it too. Sometimes it is possible to start a new thread on the server that is separate from the request thread, but if the request is aborted before it fully reaches the server, it will all fail.

如果您想在 ajax 完成之前重定向,那是可能的,但很难。例如,我不知道如何在 PHP 中执行此操作。问题是,如果您立即重定向(再次应该通过 javascript 完成),那么请求将从客户端中止。如果请求被中止,服务器也会中止它。有时可能会在服务器上启动一个与请求线程分开的新线程,但是如果请求在完全到达服务器之前被中止,它就会全部失败。

In general, you should not do this - it's a wrong approach. Either don't redirect, or don't start the ajax request on the previous page - start it as soon as the new page loads.

一般来说,你不应该这样做 - 这是一种错误的方法。要么不要重定向,要么不要在上一页上启动 ajax 请求——在新页面加载后立即启动它。

Update: I saw you need your ajax request to run for an hour - that won't happen - the browser will timeout. After you confirm that one hour is really needed, check thisfor asynchronous php tasks. You can start it from $(document).ready(function() {..});of the 2nd page

更新:我看到你需要你的 ajax 请求运行一个小时——这不会发生——浏览器会超时。在您确认确实需要一小时后,请检查异步 php 任务。你可以从$(document).ready(function() {..});第二页开始