在用户返回页面后,显示AJAX调用检索到的相同数据


Show the same data retrieved by the AJAX call after the user returns to the page

我正在尝试获取此表单的URL,并使用AJAX显示结果,一切都很好,但一旦我访问了其中一个结果的链接,我就会丢失之前在AJAX调用中检索到的所有数据。我想要的是,当用户按下浏览器上的后退按钮时,他们会在离开页面之前返回到同一页面,并使用从AJAX调用中检索到的相同数据。我正在建设一个电子商务网站。

    $(document).ready(function(){
        $("#form").change(function(){
        var form=$("#form").serialize();
            $.ajax({
                type: "GET",
                url: "get.php",
                data: form,
                success: function(html) {
                    $("#pagination").fadeOut(0).html(html).fadeIn(300);
                }
            });
        return false;
        });
    });

按下后退按钮后:

我有这个网址:search.php尽管我需要实现:search.php?brand=本田&model=etc

您可以通过操纵浏览器历史记录来实现这一点:

if (typeof (history.pushState) != "undefined") {
    var obj = { Title: title, Url: url };
    history.pushState(obj, obj.Title, obj.Url);
} else {
    alert("Browser does not support HTML5.");
}

在对象中构造您的url:

var obj = { Title: 'Title of my page', Url: 'search.php?brand='+form.brand+'&model='+form.model };

您必须将此代码放在成功回调函数中。