javascript POST 不重定向页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4677146/
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
POST without redirecting page
提问by giri
Hi I am writing an application where I want to post data after clicking send button, it will post data to some web-action and gets processed, after processing it will be redirected to some other jsp page but I want to be in the same page from where I click send button. I am not sure of XMLHttpRequest method.
嗨,我正在编写一个应用程序,我想在单击发送按钮后发布数据,它会将数据发布到某个 Web 操作并进行处理,处理后将被重定向到其他一些 jsp 页面,但我想在同一页面中从那里我点击发送按钮。我不确定 XMLHttpRequest 方法。
Any ideas to get this done highly appreciated.
任何完成这项工作的想法都非常感谢。
回答by Harry Joy
If you wanna do using Java script and Ajax (As you have in your question tags) then Try following:
如果您想使用 Java 脚本和 Ajax(如您的问题标签中所示),请尝试以下操作:
function fun() {
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
var url = "Serv?req_id=1";
xmlhttp.open("POST", url, false);
xmlhttp.send(null);
var respo= xmlhttp.responseText;
document.getElementById("some_id").innerHTML = xmlhttp.responseText;
}
Call fun()on onclickevent of send button.
调用fun()的onclick事件send button。
Hope this helps.
希望这可以帮助。
回答by Andrey
You can use jQuery.ajax()- it's a convenient wrapper for XMLHttpRequest.
您可以使用jQuery.ajax()- 它是 XMLHttpRequest 的方便包装器。
回答by fiiv
Here's a great beginner's tutorial on what you're looking to do (with jQuery):
这是一个很棒的初学者教程,介绍了您要做什么(使用 jQuery):
http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/
http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/
回答by Sarwar Erfan
You can use jQuery. It will save some time. It is cross browser compatible (i.e. hides the cross browser compatibility techniques from you).
您可以使用 jQuery。它会节省一些时间。它是跨浏览器兼容的(即对您隐藏跨浏览器兼容技术)。
http://api.jquery.com/jQuery.post/
回答by Macy Abbey
You can issue a 302 request back to the page you came from as the response from the POST request. If you don't want to POST at all I can show you how to do this through AJAX.
您可以将 302 请求返回到您来自的页面,作为 POST 请求的响应。如果您根本不想 POST,我可以向您展示如何通过 AJAX 执行此操作。
The reason you want to use the GET->POST->GET is so that if the user hits the back button it doesn't re-post on you.
您想使用 GET->POST->GET 的原因是,如果用户点击后退按钮,它不会重新发布给您。
回答by Vishal
A 302 request back approach seems to be good for this, If you were using ajax or XMLHttpRequest object, it wouldn't be that problem, but here you can set redirection header to redirect back on the same script that process your query-string.
302 请求返回方法似乎对此有好处,如果您使用的是 ajax 或 XMLHttpRequest 对象,则不会出现这个问题,但在这里您可以设置重定向标头以重定向回处理您的查询字符串的同一脚本。
However you cannot do post without a redirection.
但是,您不能在没有重定向的情况下发帖。
回答by user115422
I know this is a bit late but for the sake of sharing knowledge, I found a good article here: http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/On it, is a very thorough example of how to do what you are asking for.
我知道这有点晚了,但为了分享知识,我在这里找到了一篇好文章:http: //net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh- using-jquery/在它上面,是一个非常详尽的例子,说明如何做你所要求的。
This is done, as mentioned above, with AJAX/jQuery, this tutorial makes use of that.
如上所述,这是通过 AJAX/jQuery 完成的,本教程利用了它。

