Javascript 使用 POST AJAX 重新加载页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8268294/
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
AJAX reload page with POST
提问by artaxerxe
Can anybody tell me how to refresh the current page with JavaScript, having a POST variable modified or added?
谁能告诉我如何使用 JavaScript 刷新当前页面,修改或添加 POST 变量?
To be clear, I want to set some POST variables prior to reloading the page.
需要明确的是,我想在重新加载页面之前设置一些 POST 变量。
回答by mapet
By using jquery ajax you can reload your page
通过使用 jquery ajax,您可以重新加载页面
$.ajax({
type: "POST",
url: "packtypeAdd.php",
data: infoPO,
success: function() {
location.reload();
}
});
回答by Pranav Hosangadi
If you want to refresh the entire page, it makes no sense to use AJAX. Use normal Javascript to post the form element in that page. Make sure the form submits to the same page, or that the form submits to a page which then redirects back to that page
如果要刷新整个页面,使用AJAX是没有意义的。使用普通的 Javascript 在该页面中发布表单元素。确保表单提交到同一页面,或者表单提交到一个页面,然后重定向回该页面
Javascript to be used (always in myForm.php):
要使用的 Javascript(总是在 myForm.php 中):
function submitform()
{
document.getElementById('myForm').submit();
}
Suppose your form is on myForm.php: Method 1:
假设您的表单在 myForm.php 上:方法 1:
<form action="./myForm.php" method="post" id="myForm">
...
</form>
Method 2:
方法二:
myForm.php:
myForm.php:
<form action="./myFormActor.php" method="post" id="myForm">
...
</form>
myFormActor.php:
myFormActor.php:
<?php
//all code here, no output
header("Location: ./myForm.php");
?>
回答by vikky
Reload the current document:
重新加载当前文档:
<script type="text/javascript">
function reloadPage()
{
window.location.reload()
}
</script>
回答by Daniil Mashkin
There's another waywith post
instead of ajax
还有另一种方式与post
替代ajax
var jqxhr = $.post( "example.php", function() {
alert( "success" );
})
.done(function() {
alert( "second success" );
})
.fail(function() {
alert( "error" );
})
.always(function() {
alert( "finished" );
});