Chrome displays ajax response when pressing back button - ajax

I've come across a problem that if I use jQuery's Get method to get some content, if I click back, instead of it actually going back one page in the history, it instead shows the content returned by the Ajax query.
Any idea's?
http://www.dameallans.co.uk/preview/allanian-society/news/56/Allanian-test
On the above page, if you use the pagination below the list of comments you will notice when clicking back after changing a page, that it shows the HTML content used to generate the list of comments.
I've noticed it doesn't always do it, but if you click on a different page a few times and click the back button, it simply displays json text within the window instead of the website.
For some reason, this is only affecting Chrome as IE and Firefox work ok.

Make sure your AJAX requests use a different URL from the full HTML documents. Chrome caches the most recent request even if it is just a partial.
https://code.google.com/p/chromium/issues/detail?id=108425

Just in case you are using jQuery with History API (or some library like history.js), you should change $.getJSON to $.ajax with cache set to false:
$.ajax({
dataType: "json",
url: url,
cache: false,
success: function (json) {...}
});

Actually this is the expected behavior of caching system according to specs and not a chrome issue. The cache only differentiate requests base on URL and request method (get, post, ...), not any of the request headers.
But there is a Vary header to tell browser to consider some headers when checking the cache. For example by adding Vary:X-Requested-With to the server response the browser knows that this response vary if request X-Requested-With header is changed. Or by adding Vary:Content-Type to the server response the browser knows that this response vary if request Content-Type header is changed.
You can add this line to your router for PHP:
header('Vary:X-Requested-With');
And use a middleware in node.js:
app.use(function(req, res) {
res.header('Vary', 'X-Requested-With');
});

You can also add a random value to the end of the ajax url. This will ignore the previous chrome cache and will request a new version
url = '/?'+Math.random()

Just add the following header to the Response headers :
Vary: Accept

I couldn't give different urls for each ajax request as it was an ajax pagination, declaring no cache on headers did nothing, so i included a little javascript in the view only when headers were for the ajax request:
<script>
if (typeof jQuery == 'undefined') {
window.location = "<?php echo $this->here; ?>";
}
</script>
It is a dirty trick, but it works, if the ajax content is normally loaded, the container has Jquery loaded so it does nothing. But if you load the ajax supposed content without the surrounding content, Jquery is missing (at least in my case), so i redirect to the current page requesting a normal GET page with all the headers and scripts.
If you put it in the top of the page, the user won't notice because it won't wait till the page loads, it will redirect as soon as the browser gets this 4 lines...
Replace here; ?> by the current url in your APP, this was a CakePhp 2.X

Still had this problem in 2021 in Chrome.
Problem is doing underlying ajax request to the same url as the one the user is currently on.
I was working in Symfony and the complete fix that did the work for me was
$response->headers->addCacheControlDirective('no-cache', true);
$response->headers->addCacheControlDirective('max-age', 0);
$response->headers->addCacheControlDirective('s-maxage', 0);
$response->headers->addCacheControlDirective('must-revalidate', true);
$response->headers->addCacheControlDirective('no-store', true);
/**
* from https://stackoverflow.com/a/1975677/5418514
*
* The HTTP request header 'Accept' defines the Content-Types a client can process.
* If you have two copies of the same content at the same URL, differing only in Content-Type,
* then using Vary: Accept could be appropriate.
*/
$response->headers->set('Vary', 'Accept');

The #abraham's answer is right.
I just wanted to post a solution for Rails: all you need is just add different path to routes.rb.
In example, I have resource :people and I want to compose index page from ajax parts one of those is list of people. The straightforward way is to create index.js.erb and to load partial via ajax using url: people_path. But here occurs the issue.
So, for Rails, it needs just add a different route, like
get 'people_list', to: 'people#index', as: :people_list, format: :js

If I want to use index method of a laravel controller returns both html and json response, I add a get parameter at the end of the endpoint to pass browser caching:
axios.get(url, {params: {ajax: 1}})

Related

php redirect to another page in laravel

I have a modal view (the one from bootstrap) in the front end.
Upon clicking the submit button the user will be going to a function in controller:
Route::post('post_question', array('uses' => 'QuestionController#postQuestion'));
And at the end of the postQuestion i want to redirect to another page.
I tried:
return Redirect::to('mywebsite/question/1');
return Response::make( '', 302 )->header( 'Location', 'mywebsite/question/1' );
return Redirect::route('question/'.$question_id)->with("question",Question::find($question_id));
header("Location: mywebsite/question/$question_id");
none seem to work though.
The thing is, i can see the request in XHR but just that the page is not redirected.
Is the modal somehow blocking the behavior?
You can redirect with an AJAX request. However, you will find that the results will not be quite what you expected.
On a redirect, Laravel will should set your response code header as a redirect response and then the content of the redirected page would be sent.
You could do one of two things depending on how you wanted to handle things.
Send a JSON response back to the submitted form with a meta data parameter and then use this meta data in your success function to set window.location.
Your Laravel controller responding to the post would look a bit like this:
public function postQuestion()
{
// DO stuff to set your $question
return [
'question' => $question,
'meta' => [
'redirect_url' => url('mywebsite/question/'.$question->id),
'status' => '400',
// Any other meta data you may want to send
],
];
}
Then assuming you are doing some jQuery AJAX call, change your success callback (I'm calling it questionSubmitSuccess here):
questionSubmitSuccess = function (data) {
// Anything you may want to do before redirecting the user
if (data.meta.redirect_url) {
// This redirects the page
window.location = data.meta.redirect_url;
}
}
Continue redirecting from your controller and then do something a bit more similar to rails turbo links and replace the entire page with Javascript:
You can do this a few ways: using [Modify the URL without reloading the page browser History API), or using jQuery.load to submit your form.
The browser history API might work a bit easier as it would still allow you to handle response errors, but it only works in more modern browsers.
jQuery.load would likely require rewriting a bit of your AJAX submitting code and is harder to handle things like errors (it will replace your page content no matter the status code from what I can tell), but it has better browser support.
IMO, the first approach is a bit more maningful as the API endpoint is usable by something other than this single implementation.
Also, there are fewer points of failure and error states to manage compared to trying to replace your entire DOM without a page reload.
U can put button inside form and when u submit that button pass data from page to controller and from controller call the another page with that data
like return View::make('users.index,compact('data'));

Is it possible to get a part of page in an ajax get request?

I know we can fetch a whole page when doing a GET request to the server, but what if i'm only interested in one particular div on that page, or more exactly in its content. Is the only option here is to fetch the whole page, get the div content from it using jquery find() for example? Or is there some possibility to set up some kind of query to the server to ask for this particular div to return.
$.ajax({
'type': 'get'
...some params...
success: function(response){
$(response).find('div you are looking for').appendTo('div you want to append it');
}
});
For some params check jQuery ajax.
And answering your question: no, there is no way to get some parts of HTML without making special server-side service.
You might want to use a simpler form of jQuery's AJAX tool for this situation, .load.
$('#wrapper').load(href+fragmentSelector, function() {
// callback to do stuff afterward
});
Here, we're loading content from the element with ID fragmentSelector from the url href into #wrapper on our current page.
The entire page is still being requested, but only the content you request is returned.
Reference: http://api.jquery.com/load/#loading-page-fragments
You can use HTTP RANGE Header, but you can select a byte range to download and not a select content by DIV.
Example:
curl -r 0-1000 http://localhost -v
see Using the HTTP Range Header with a range specifier other than bytes?

Ajax submit redirect MVC 3

Although I read dozens of answers I could not find a solution.
I'm using MVC 3 with Razor. I have a simple Form with client validation via ajax. This part works fine. My problem is: In the update Action of my Controller I want to redirect to another Action. If the user disables Javacript, this works fine. But with javascript/ajax enabled, the redirectaction doesn't seem to work. Instead it looks like if some kind of partialview or something like that is executed.
My Controller/Action-code:
Function UpdateItem(Item As CItem) As ActionResult
' some validation code, save etc.
if everythingok then
Return RedirectToAction("Updatesuccess")
else
Return RedirectToAction("EditItem")
endif
End Function
My html page looks like (shortened/pseudo):
Logo image
H1
some text
<form>....</form>
When the form is submitted via ajax the new html code is added beyond "some text", so the form is replaced but everything above the replaced form stays on the page.
When ajax/javascript is disabled then after submit a whole new page is loaded. I checked the http headers and noticed, that with ajax there is no redirect (which is logical in some way because it is ajax).
What can I do? I want to redirect to a new page.
Is it possible to disable the ajax-submit and only use the "normal" form-submit?
I like the client validation via ajax while the user enters data and I want to use this, but for me it would be good enough, if the submit would be a "normal" submit.
I hope someone understands what I want and can help me.
Thanks.
I'm not a 100% sure I understand your question, but what the heck, don't downvote me :)
The problem is, redirect works via sending a HttpResponse with the redirect indicated in the headers, which the browser understands. If you submit via AJAX, it's not the browser that handles the request, it's your OWN JS code.
Here is the trick: instead of returning a redirect, return the Url (preferably as json), and then redirect manually using window.location.
I'm not fluent in VB, here is how I'd do it in C#:
var url = new UrlHelper(this.ControllerContext.RequestContext);
var link = url.Action("UpdateSuccess");
return Json(new {link});
and then in jQuery:
$.ajax({ method: 'POST',
url: 'your post url',
success: function(result) {window.location = result.link; },
// ... (passing form etc)
});

Go - How to load a new html form

After processing a jQuery Ajax Post from an HTML form successfully within a Go program, how do I load a new form? I first tried sending the form as the response and the Javascript displayed it, but it did not clear the old (existing) form. I then tried within the HTML Javascript to set the URL using "window.location = 'localhost:8088/MaintForm/'". That resulted in a warning from the browser but did not load the form and did not change the URL. I would like to ideally know both methods - via the Go program acting as a server, and via Javascript. If I manually change the URL, the form loads OK. What I am trying to do is receive a response in Javascript (jQuery Ajax), and then request the new form if the response is positive. I would prefer to do this without changing the URL. As I said above, this partially worked.
You would have to put your original form inside a tag, for example a div, and use your JQuery code to replace the contents of that tag with the new form. This way you are not changing the URL.
This is more of a javascript/JQuery question than a go-specific one.
In javascript:
location.href = '/MaintForm/';
In golang, you can use the http.Redirect function, like this:
http.Redirect(w, r, "/MaintForm/", http.StatusFound)
Please note: this appears to be solved by : I just need to do an "document.write(data);" in Javascript. "Data" contains the new HTML.

IE browser caching and the jQuery Form Plugin

Like so many lost souls before me, I'm floundering in the snake pit that is Ajax form submission and IE browser caching.
I'm trying to write a simple script using the jQuery Form Plugin to Ajaxify Wordpress comments. It's working fine in Firefox, Chrome, Safari, et. al., but in IE, the response text is cached with the result that Ajax is pulling in the wrong comment.
jQuery(this).ajaxSubmit({
success:
function(data) {
var response = $("<ol>"+data+"</ol>");
response.find('.commentlist li:last').hide().appendTo(jQuery('.commentlist')).slideDown('slow');
}
});
ajaxSubmit sends the comment to wp-comments-post.php, which inelegantly spits back the entire page as a response. So, despite the fact that it's ugly as toads, I'm sticking the response text in a variable, using :last to isolate the most recent comment, and sliding it down in its place.
IE, however, is returning the cached version of the page, which doesn't include the new comment. So ".commentlist li:last" selects the previous comment, a duplicate of which then uselessly slides down beneath the original.
I've tried setting "cache: false" in the ajaxSubmit options, but it has no effect. I've tried setting a url option and tacking on a random number or timestamp, but it winds up being attached to the POST that submits the comment to the server rather than the GET that returns the response, and so has no effect. I'm not sure what else to try. Everything works fine in IE if I turn off browser caching, but that's obviously not something I can expect anyone viewing the page to do.
Any help will be hugely appreciated. Thanks in advance!
EDIT WITH A PROGRESS REPORT: A couple of people have suggested using PHP headers to prevent caching, and this does indeed work. The trouble is that wp-comments-post is spitting back the entire page when a new comment is submitted, and the only way I can see to add headers is to put them in the Wordpress post template, which disables caching on all posts at all times--not quite the behavior I'm looking for.
Is there a way to set a php conditional--"if is_ajax" or something like that--that would keep the headers from being applied during regular pageloads, but plug them in if the page was called by an Ajax GET?
jQuery.ajaxSubmit() takes any of the options for the standard jQuery.ajax(). You can thus use the standard cache: false option to turn off caching:
jQuery(this).ajaxSubmit({
cache: false,
success:
function(data) {
var response = $("<ol>"+data+"</ol>");
response.find('.commentlist li:last').hide().appendTo(jQuery('.commentlist')).slideDown('slow');
}
});
The way I have been doing this is by adding a rand=new Date().getTime() to the end
if(url.replace("?") != url)
url = url+"&rand="+new Date().getTime();
else
url = url+"?rand="+new Date().getTime();
The function above will append the rand=time to the address of the url [address to the .php] If you have supplied get parameters, it will add &rand=time... otherwise it will add ?rand=time
The browser keeps caching, but the pages won't overlap.
You could also use PHP's header() to disable caching by setting Cache-control: and Expires:
Put this in the beginning of Your php:
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
This should help. If it doesn't - try putting a random number as a filename with the headers.
The post is redirected to a get request and You would have to send some info to the get page to control if it should be cached or not.
This will prevent caching globally
$(document).ready(function() {
$.ajaxSetup({ cache: false });
});

Resources