Getting error 304 in response to jquery request
Scenario:
-
User visits the page
-
Jquery
started making a requestajax
for the backend code - Go to the page (not refreshing). I can see that the request is still going to the server from the client. It should now be stopped.
- When the user comes back to the same page again, the request should start hitting, but it quits
304 error
. Jquery assumed it would submit the request again.
User clicks the following code button
index.html.erb
<td colspan="3"><%= link_to 'Show state', simulation, :class => 'btn btn-primary' %></td>
will navigate
show.html.erb
Here I wrote some code Jquery
to make a requestajax
<script>
$(function() {setInterval(function(){ $.get("/posts/<%= @post.id %>"); }, 5000);
});
Dom needed to be changes after every `Ajax` response in same views (show.html.erb)
<span id="post-data">
<table class = 'table table-hover'>
.....
</table>
</span>
js file for rendering views
in show.js.erb
$("#post-data").html(" <table> ... </table>")
How can I fix these errors. Any help would be appreciated, I am not the key javascript
guy.
What am I looking for?
- A way to stop the event
Jquery
when the user navigates to another page (by clicking the "Back" button) - A way to run
Jquery
even when the user returns to the page.
source to share
When it is submitted a second time, it will return the cached content from the browser as stated in the spec:
For 304 Unchanged responses that are the result of the generated conditional request by the user agent, the user agent should act as if the server responded with a 200 OK response with the appropriate content.
jQuery doesn't generate any responses here. And header 304 is just an HTTP header. HTTP AJAX requests are regular HTTP requests and can return any valid header. If the server responds with 304, the XHR entity will simply serve the locally cached response from the server. This is completely transparent to the user.
You can use cache parameter in ajax request then it can work:
$.ajax({
url: ".....",
dataType: "json",
type: "GET",
cache: false,
contentType: "application/json",
success: function (data, textStatus) {
console.log("RECV: " + data);
}
});
source to share