Moyed Ansari
Moyed Ansari

Reputation: 8461

Get Ajax Previous Request with Browser Back Button

I’m facing a situation with Ajax request on back button.

I created a form which posts values and returns results via ajax request according the given filters and loads on a specific div. When I click on any of the link on page, it opens the new url as expected. 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.

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.

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.

Upvotes: 0

Views: 1906

Answers (3)

Roman Slobodzyan
Roman Slobodzyan

Reputation: 311

You should set cache to false:

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

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

Upvotes: 2

Þaw
Þaw

Reputation: 2057

you can use Jquery .unload() function or try cookies you could read it from here How do I set/unset cookie with jQuery?

Upvotes: 0

tech savy
tech savy

Reputation: 193

Once you submitting that time dont go for new page, just hide the form elements and display new content in other div. Once you click on back button just show previously hidden div and hide current showing div

Upvotes: 0

Related Questions