[英]Laravel request()->ajax() triggering on browser back button
Inside my controller I have this function for the route /backups
在我的控制器内部,我具有用于路由/backups
public function index()
{
$backups = \App\Backup::all();
if(request()->ajax()) {
return $backups;
}
return view('backups.index', compact('backups'));
}
My idea was that if I have my javascript ask for the data then return json
if not return html
. 我的想法是,如果我有我的JavaScript要求数据,那么如果不返回html
则返回json
。
This works fine, except when pressing the browser back button to go from lets say /backups/1
to /backups
it shows the json
. 这很好用,除非按下浏览器的后退按钮从说/backups/1
到/backups
时显示json
。
Is there another function I can use that will only respond to ajax calls from my code and not the browsers? 我是否可以使用另一个仅响应来自我的代码而不响应浏览器的ajax调用的功能?
Make sure your AJAX requests use a different URL from the full HTML documents. 确保您的AJAX请求使用与完整HTML文档不同的URL。 Chrome (and most probably Firefox) caches the most recent request even if it is just a partial. Chrome(很可能是Firefox)会缓存最近的请求,即使只是部分请求也是如此。
Source: 资源:
https://code.google.com/p/chromium/issues/detail?id=108425 https://code.google.com/p/chromium/issues/detail?id=108425
Or: 要么:
Try setting cache to false 尝试将缓存设置为false
$.ajax({
dataType: "json",
url: url,
cache: false,
success: function (data) {...}
});
I'd recommend adding an ajax-only query string parameter to the ajax request, eg ?ajax=1
. 我建议向ajax请求中添加一个仅ajax的查询字符串参数,例如?ajax=1
。
This way, you can 1. utilise the browser cache, and 2. keep the same Laravel route for both request types. 这样,您可以1.利用浏览器缓存,并2.为两种请求类型保留相同的Laravel路由。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.