简体   繁体   English

如何在AJAX调用后重定向/刷新页面

[英]How to redirect/refresh the page after an AJAX call

在AJAX调用之后,我对PHP文件进行了更新,但是我不确定,一旦请求完成,如果页面上的PHP标头位置刷新了我所在的页面,哪种方法更好? ,或在AJAX中完成。刷新页面。

  1. If you are doing Ajax query to other page, then header("location: ...") won't work as it can not refresh current page. 如果您要对其他页面进行Ajax查询,则header("location: ...")将无法工作,因为它无法刷新当前页面。

  2. If you are making an Ajax request to the same page, don't do that. 如果要向同一页面发出Ajax请求,请不要这样做。 Instead submit the form the old school way. 而是以旧的方式提交表格。

  3. If you are making an Ajax request to other page that does something essential for you and then you want to refresh the current page then use the given JavaScript code in AJAX.done. 如果您正在向其他页面发出Ajax请求,而该页面对您做了一些必不可少的事情,然后您想刷新当前页面,则使用AJAX.done中的给定JavaScript代码。

If you want to preserve cache and reload(soft reload) which is a better way, use: 如果要保留缓存和重新加载(软重新加载),这是一种更好的方法,请使用:

location.reload(false);

However If you have requirement to clear cache and reload(hard reload), use: 但是,如果您需要清除缓存并重新加载(硬重新加载),请使用:

location.reload(true);

Hope this helps. 希望这可以帮助。

You can find more detail about location.reload here: https://developer.mozilla.org/en-US/docs/Web/API/Location.reload 您可以在此处找到有关location.reload更多详细信息: https : //developer.mozilla.org/en-US/docs/Web/API/Location.reload

If you want to refresh the whole page then don't even use an ajax request, submit your form, and php redirect will be fine. 如果您想刷新整个页面,那么甚至不使用ajax请求,提交您的表单,php重定向就可以了。

If you want to refresh a part of the page (for instance the content of a div ), then you should do it client-side (in Ajax.done ) 如果要刷新页面的一部分(例如div的内容),则应在客户端(在Ajax.done )进行Ajax.done

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

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