简体   繁体   English

使用浏览器后退按钮获取Ajax先前的请求

[英]Get Ajax Previous Request with Browser Back Button

I'm facing a situation with Ajax request on back button. 我面对后退按钮上的Ajax请求的情况。

I created a form which posts values and returns results via ajax request according the given filters and loads on a specific div. 我创建了一个表单,该表单根据给定的过滤器通过ajax请求发布值并返回结果,并在特定的div上加载。 When I click on any of the link on page, it opens the new url as expected. 当我单击页面上的任何链接时,它将按预期方式打开新的URL。 On clicking on browser back button it opens the previous form page with default values. 在单击浏览器后退按钮时,它将打开具有默认值的上一个表单页面。 How can I enable browser state functionality so that I have results with last posted values with browser back button. 如何启用浏览器状态功能,以便使用浏览器后退按钮获得最后发布值的结果。 Note that type of ajax is POST . 注意,ajax的类型是POST

One solution I got is that to modify this form type to GET instead of POST, but this would take much time to do changes in server side code. 我得到的一个解决方案是将这种表单类型修改为GET而不是POST,但这将花费很多时间来更改服务器端代码。

var page_url = $(this).attr('href');
page_url = page_url.split(':');
var page = page_url['1'];

$form = $('#form);

        $.ajax({
        type: 'POST',
        data: $form.serialize(),
        url: webroot + 'controller /action/page:'+page
        }).done(function (result){
})

I want to know the possible solution. 我想知道可能的解决方案。

You should set cache to false: 您应该将cache设置为false:

$.ajax({
    dataType: "json",
    url: url,
    cache: false,
    success: function (json) {...}
});

Source https://stackoverflow.com/a/25230377 来源https://stackoverflow.com/a/25230377

您可以使用Jquery .unload()函数或尝试使用cookies您可以从此处读取它。 如何使用jQuery设置/取消设置cookie?

Once you submitting that time dont go for new page, just hide the form elements and display new content in other div. 提交该时间后,请勿进入新页面,只需隐藏表单元素并在其他div中显示新内容即可。 Once you click on back button just show previously hidden div and hide current showing div 一旦单击后退按钮,仅显示以前隐藏的div并隐藏当前显示的div

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

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