简体   繁体   English

Javascript:发布请求,然后将页面重定向到另一个页面

[英]Javascript : Posting a request and then redirecting the page to another page

I have a use case where i have to post the form data on a particular link and then forward/redirect the request to another link. 我有一个用例,其中我必须在特定链接上发布表单数据,然后将请求转发/重定向到另一个链接。 For eg In my page i have 3 textfields a,b,c and a submit button. 例如,在我的页面中,我有3个文本字段a,b,c和一个提交按钮。 On click of Submit i will post the request to say "http://www.abc.com/example" and then redirect the request to "http://www.def.com". 在单击“提交”时,我将发布要求说“ http://www.abc.com/example”,然后将请求重定向到“ http://www.def.com”。

So for the end user after submitting the request he/she will see "http://www.def.com" and will not come to know that what happened in between. 因此,对于最终用户,在提交请求后,他/她将看到“ http://www.def.com”,并且不会知道这之间发生了什么。

We do not care what the response is from server where request is posted. 我们不在乎来自发布请求的服务器的响应是什么。

Any help/directions in implementing this use case will be highly appreciated. 高度赞赏实现此用例的任何帮助/指导。

Thanks. 谢谢。

Regards, Mayank 此致Mayank

Just specify the submit URL in form's action . 只需在表单的action指定提交URL。

<form action="http://www.abc.com/example" method="post">

and let the code behind that URL redirect the request to the desired URL after postprocessing the request; 在对该请求进行后处理之后,让该URL后面的代码将请求重定向到所需的URL; the following example assumes that it's using the Servlet API: 以下示例假定它正在使用Servlet API:

response.sendRedirect("http://www.def.com");

No need for JavaScript here which would not work anyway on JS-disabled clients. 这里不需要JavaScript,因为JavaScript不能在禁用JS的客户端上正常运行。

It would be preferable for performance to submit the form to your original server and then redirect to the other URL. 为了提高性能,最好将表单提交到原始服务器,然后重定向到另一个URL。

If you can't do this, then you could submit the request via AJAX request and on completion (success or error) change the window.location to the other URL. 如果您无法执行此操作,则可以通过AJAX请求提交请求,并在完成(成功或错误)后将window.location更改为另一个URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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