jquery:window.location.reload() 不允许工作 $.post()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3085856/
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
jquery: window.location.reload() doesn't allow to work $.post()
提问by Simon
look at this script please
请看这个脚本
$("#change").click(function()
{
var val = $("#new_title").val();
if(val == '')
{
alert("???? ?????? ??????");
return false;
}
else
{
$.post
(
"change_title.php",
{id: id, lang: lang, val: val}
);
window.location.reload();
}
});
where id
and lang
are global variables.
其中id
和lang
是全局变量。
in change_title.php i'm uploading the table.
在 change_title.php 中,我正在上传表格。
i want to show changes after editing, so i use window.location.reload();
function, but it doesn't work. if i delete window.location.reload();
function, it works fine.
我想在编辑后显示更改,所以我使用 window.location.reload();
函数,但它不起作用。如果我删除window.location.reload();
函数,它工作正常。
what is the problem?
问题是什么?
Thanks
谢谢
回答by Nick Craver
You need to run it after the $.post()
completes, like this:
您需要在$.post()
完成后运行它,如下所示:
$.post("change_title.php",
{id: id, lang: lang, val: val},
function() {window.location.reload(); });
Without doing this as the callback to $.post()
(this runs when it completes), the window is leaving the page beforethe POST completes. If you don't need to do anything else in that function, you can shorten it down to:
如果不这样做作为回调$.post()
(当它完成时运行),窗口将在 POST 完成之前离开页面。如果您不需要在该函数中执行任何其他操作,则可以将其缩短为:
$.post("change_title.php",
{id: id, lang: lang, val: val},
window.location.reload);
回答by Aaron Harun
You need to use a timeout on window.location.reload
or use a callback function. The post isn't being given enough time to be sent.
您需要使用超时window.location.reload
或使用回调函数。没有足够的时间发送帖子。