Javascript (jQuery) loaded JSON doesn't update unless I refresh the JSON file in my browser
up vote
0
down vote
favorite
So I'm loading the JSON file movies.json so I can get its data. The data is submitted via a POST request which I can confirm automatically updates it just fine. Anyways, when I set the values as a Javascript Array, it doesn't automatically update on the page. The console output shows the previous JSON, for example. To resolve this, I am required to manually go to the json file in my browser, and refresh the page. And then when I refresh the page where I wish to show my json values, it works. Can anyone help me figure this out? I'm fairly new to Javascript.
$(document).ready(function) {
$.getJSON("{{ url_for('static', filename='movies.json') }}", function(json){
var data = ;
$.each(json, function (index, value) {
data.push(value); // Puts the JSON values into an array
});
console.log(data) // Logs the Array
var output = document.getElementById('movie'); // Sets output to the movie div
output.innerHTML = data[0]; // Sets the value to the title of the movie
});
});
This outputs, for example, "Avengers: Infinity war", which can also be seen in my JSON file. But if I send another post request for say, "Batman and Robin", it still outputs "Avengers: Infinity war", while my actual JSON file accurately says "Batman and Robin"
javascript jquery json
|
show 2 more comments
up vote
0
down vote
favorite
So I'm loading the JSON file movies.json so I can get its data. The data is submitted via a POST request which I can confirm automatically updates it just fine. Anyways, when I set the values as a Javascript Array, it doesn't automatically update on the page. The console output shows the previous JSON, for example. To resolve this, I am required to manually go to the json file in my browser, and refresh the page. And then when I refresh the page where I wish to show my json values, it works. Can anyone help me figure this out? I'm fairly new to Javascript.
$(document).ready(function) {
$.getJSON("{{ url_for('static', filename='movies.json') }}", function(json){
var data = ;
$.each(json, function (index, value) {
data.push(value); // Puts the JSON values into an array
});
console.log(data) // Logs the Array
var output = document.getElementById('movie'); // Sets output to the movie div
output.innerHTML = data[0]; // Sets the value to the title of the movie
});
});
This outputs, for example, "Avengers: Infinity war", which can also be seen in my JSON file. But if I send another post request for say, "Batman and Robin", it still outputs "Avengers: Infinity war", while my actual JSON file accurately says "Batman and Robin"
javascript jquery json
Take a look at the response headers of the JSON in Network tab of DevTools. How are caching headers delivered, or are they present at all? You should find at leastcache-control: no-cache, no-store, must-revalidate
header.
– Teemu
Nov 15 at 11:58
1
Well you should create a function to read the json data and whenever you are making post request to save data in json , just call that fucntion on sucess of save post , that should solve your issue
– siddhant sankhe
Nov 15 at 12:04
Since I have absolutely no idea what a response header is, I'd say they are not present. I did check the network tab, and there was nothing there. But I do know that accessing the JSON values through Flask (with open, etc) and Jinja2 updates normally.
– Jertyu
Nov 15 at 12:04
1
Headers are small pieces of information your server sends to browser as a part of the response to a HTTPRequest. Open the tab, make the AJAX call run on you page, it should appear in the list on the Network tab (Activate XHR-filter if needed) Then click the AJAX row, and see the headers on the right side of the tab.
– Teemu
Nov 15 at 12:07
"Since I have absolutely no idea what a response header is, I'd say they are not present. "...that's an interesting kind of logic. How can you assert that something is not there when you don't know how to identify it? If someone asked you if there was anything wooden in the room, and you didn't know what wood was, would you say "no, there's none here", or would you say "I need to find about about wood first before I answer"
– ADyson
Nov 15 at 12:39
|
show 2 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
So I'm loading the JSON file movies.json so I can get its data. The data is submitted via a POST request which I can confirm automatically updates it just fine. Anyways, when I set the values as a Javascript Array, it doesn't automatically update on the page. The console output shows the previous JSON, for example. To resolve this, I am required to manually go to the json file in my browser, and refresh the page. And then when I refresh the page where I wish to show my json values, it works. Can anyone help me figure this out? I'm fairly new to Javascript.
$(document).ready(function) {
$.getJSON("{{ url_for('static', filename='movies.json') }}", function(json){
var data = ;
$.each(json, function (index, value) {
data.push(value); // Puts the JSON values into an array
});
console.log(data) // Logs the Array
var output = document.getElementById('movie'); // Sets output to the movie div
output.innerHTML = data[0]; // Sets the value to the title of the movie
});
});
This outputs, for example, "Avengers: Infinity war", which can also be seen in my JSON file. But if I send another post request for say, "Batman and Robin", it still outputs "Avengers: Infinity war", while my actual JSON file accurately says "Batman and Robin"
javascript jquery json
So I'm loading the JSON file movies.json so I can get its data. The data is submitted via a POST request which I can confirm automatically updates it just fine. Anyways, when I set the values as a Javascript Array, it doesn't automatically update on the page. The console output shows the previous JSON, for example. To resolve this, I am required to manually go to the json file in my browser, and refresh the page. And then when I refresh the page where I wish to show my json values, it works. Can anyone help me figure this out? I'm fairly new to Javascript.
$(document).ready(function) {
$.getJSON("{{ url_for('static', filename='movies.json') }}", function(json){
var data = ;
$.each(json, function (index, value) {
data.push(value); // Puts the JSON values into an array
});
console.log(data) // Logs the Array
var output = document.getElementById('movie'); // Sets output to the movie div
output.innerHTML = data[0]; // Sets the value to the title of the movie
});
});
This outputs, for example, "Avengers: Infinity war", which can also be seen in my JSON file. But if I send another post request for say, "Batman and Robin", it still outputs "Avengers: Infinity war", while my actual JSON file accurately says "Batman and Robin"
javascript jquery json
javascript jquery json
edited Nov 15 at 12:00
asked Nov 15 at 11:57
Jertyu
315
315
Take a look at the response headers of the JSON in Network tab of DevTools. How are caching headers delivered, or are they present at all? You should find at leastcache-control: no-cache, no-store, must-revalidate
header.
– Teemu
Nov 15 at 11:58
1
Well you should create a function to read the json data and whenever you are making post request to save data in json , just call that fucntion on sucess of save post , that should solve your issue
– siddhant sankhe
Nov 15 at 12:04
Since I have absolutely no idea what a response header is, I'd say they are not present. I did check the network tab, and there was nothing there. But I do know that accessing the JSON values through Flask (with open, etc) and Jinja2 updates normally.
– Jertyu
Nov 15 at 12:04
1
Headers are small pieces of information your server sends to browser as a part of the response to a HTTPRequest. Open the tab, make the AJAX call run on you page, it should appear in the list on the Network tab (Activate XHR-filter if needed) Then click the AJAX row, and see the headers on the right side of the tab.
– Teemu
Nov 15 at 12:07
"Since I have absolutely no idea what a response header is, I'd say they are not present. "...that's an interesting kind of logic. How can you assert that something is not there when you don't know how to identify it? If someone asked you if there was anything wooden in the room, and you didn't know what wood was, would you say "no, there's none here", or would you say "I need to find about about wood first before I answer"
– ADyson
Nov 15 at 12:39
|
show 2 more comments
Take a look at the response headers of the JSON in Network tab of DevTools. How are caching headers delivered, or are they present at all? You should find at leastcache-control: no-cache, no-store, must-revalidate
header.
– Teemu
Nov 15 at 11:58
1
Well you should create a function to read the json data and whenever you are making post request to save data in json , just call that fucntion on sucess of save post , that should solve your issue
– siddhant sankhe
Nov 15 at 12:04
Since I have absolutely no idea what a response header is, I'd say they are not present. I did check the network tab, and there was nothing there. But I do know that accessing the JSON values through Flask (with open, etc) and Jinja2 updates normally.
– Jertyu
Nov 15 at 12:04
1
Headers are small pieces of information your server sends to browser as a part of the response to a HTTPRequest. Open the tab, make the AJAX call run on you page, it should appear in the list on the Network tab (Activate XHR-filter if needed) Then click the AJAX row, and see the headers on the right side of the tab.
– Teemu
Nov 15 at 12:07
"Since I have absolutely no idea what a response header is, I'd say they are not present. "...that's an interesting kind of logic. How can you assert that something is not there when you don't know how to identify it? If someone asked you if there was anything wooden in the room, and you didn't know what wood was, would you say "no, there's none here", or would you say "I need to find about about wood first before I answer"
– ADyson
Nov 15 at 12:39
Take a look at the response headers of the JSON in Network tab of DevTools. How are caching headers delivered, or are they present at all? You should find at least
cache-control: no-cache, no-store, must-revalidate
header.– Teemu
Nov 15 at 11:58
Take a look at the response headers of the JSON in Network tab of DevTools. How are caching headers delivered, or are they present at all? You should find at least
cache-control: no-cache, no-store, must-revalidate
header.– Teemu
Nov 15 at 11:58
1
1
Well you should create a function to read the json data and whenever you are making post request to save data in json , just call that fucntion on sucess of save post , that should solve your issue
– siddhant sankhe
Nov 15 at 12:04
Well you should create a function to read the json data and whenever you are making post request to save data in json , just call that fucntion on sucess of save post , that should solve your issue
– siddhant sankhe
Nov 15 at 12:04
Since I have absolutely no idea what a response header is, I'd say they are not present. I did check the network tab, and there was nothing there. But I do know that accessing the JSON values through Flask (with open, etc) and Jinja2 updates normally.
– Jertyu
Nov 15 at 12:04
Since I have absolutely no idea what a response header is, I'd say they are not present. I did check the network tab, and there was nothing there. But I do know that accessing the JSON values through Flask (with open, etc) and Jinja2 updates normally.
– Jertyu
Nov 15 at 12:04
1
1
Headers are small pieces of information your server sends to browser as a part of the response to a HTTPRequest. Open the tab, make the AJAX call run on you page, it should appear in the list on the Network tab (Activate XHR-filter if needed) Then click the AJAX row, and see the headers on the right side of the tab.
– Teemu
Nov 15 at 12:07
Headers are small pieces of information your server sends to browser as a part of the response to a HTTPRequest. Open the tab, make the AJAX call run on you page, it should appear in the list on the Network tab (Activate XHR-filter if needed) Then click the AJAX row, and see the headers on the right side of the tab.
– Teemu
Nov 15 at 12:07
"Since I have absolutely no idea what a response header is, I'd say they are not present. "...that's an interesting kind of logic. How can you assert that something is not there when you don't know how to identify it? If someone asked you if there was anything wooden in the room, and you didn't know what wood was, would you say "no, there's none here", or would you say "I need to find about about wood first before I answer"
– ADyson
Nov 15 at 12:39
"Since I have absolutely no idea what a response header is, I'd say they are not present. "...that's an interesting kind of logic. How can you assert that something is not there when you don't know how to identify it? If someone asked you if there was anything wooden in the room, and you didn't know what wood was, would you say "no, there's none here", or would you say "I need to find about about wood first before I answer"
– ADyson
Nov 15 at 12:39
|
show 2 more comments
1 Answer
1
active
oldest
votes
up vote
0
down vote
So I solved the problem. I found that my JSON file was being cached, and that clearing my cache fixed the problem. So I looked around and found this very informative article.
So I had to add a unique ID to my json file every time it is searched for. My updated code looks like this:
$.getJSON("{{ url_for('static', filename='movies.json') }}?id={{ movie_id }}", function(json) {
When setting the response headers correctly on the server, you don't need unique query string for the filename. Make your life easy, and study the headers, if you're planning a full-stack developer carrier, you have to learn the use of the headers anyway.
– Teemu
Nov 15 at 13:35
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
So I solved the problem. I found that my JSON file was being cached, and that clearing my cache fixed the problem. So I looked around and found this very informative article.
So I had to add a unique ID to my json file every time it is searched for. My updated code looks like this:
$.getJSON("{{ url_for('static', filename='movies.json') }}?id={{ movie_id }}", function(json) {
When setting the response headers correctly on the server, you don't need unique query string for the filename. Make your life easy, and study the headers, if you're planning a full-stack developer carrier, you have to learn the use of the headers anyway.
– Teemu
Nov 15 at 13:35
add a comment |
up vote
0
down vote
So I solved the problem. I found that my JSON file was being cached, and that clearing my cache fixed the problem. So I looked around and found this very informative article.
So I had to add a unique ID to my json file every time it is searched for. My updated code looks like this:
$.getJSON("{{ url_for('static', filename='movies.json') }}?id={{ movie_id }}", function(json) {
When setting the response headers correctly on the server, you don't need unique query string for the filename. Make your life easy, and study the headers, if you're planning a full-stack developer carrier, you have to learn the use of the headers anyway.
– Teemu
Nov 15 at 13:35
add a comment |
up vote
0
down vote
up vote
0
down vote
So I solved the problem. I found that my JSON file was being cached, and that clearing my cache fixed the problem. So I looked around and found this very informative article.
So I had to add a unique ID to my json file every time it is searched for. My updated code looks like this:
$.getJSON("{{ url_for('static', filename='movies.json') }}?id={{ movie_id }}", function(json) {
So I solved the problem. I found that my JSON file was being cached, and that clearing my cache fixed the problem. So I looked around and found this very informative article.
So I had to add a unique ID to my json file every time it is searched for. My updated code looks like this:
$.getJSON("{{ url_for('static', filename='movies.json') }}?id={{ movie_id }}", function(json) {
answered Nov 15 at 13:29
Jertyu
315
315
When setting the response headers correctly on the server, you don't need unique query string for the filename. Make your life easy, and study the headers, if you're planning a full-stack developer carrier, you have to learn the use of the headers anyway.
– Teemu
Nov 15 at 13:35
add a comment |
When setting the response headers correctly on the server, you don't need unique query string for the filename. Make your life easy, and study the headers, if you're planning a full-stack developer carrier, you have to learn the use of the headers anyway.
– Teemu
Nov 15 at 13:35
When setting the response headers correctly on the server, you don't need unique query string for the filename. Make your life easy, and study the headers, if you're planning a full-stack developer carrier, you have to learn the use of the headers anyway.
– Teemu
Nov 15 at 13:35
When setting the response headers correctly on the server, you don't need unique query string for the filename. Make your life easy, and study the headers, if you're planning a full-stack developer carrier, you have to learn the use of the headers anyway.
– Teemu
Nov 15 at 13:35
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318996%2fjavascript-jquery-loaded-json-doesnt-update-unless-i-refresh-the-json-file-in%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Take a look at the response headers of the JSON in Network tab of DevTools. How are caching headers delivered, or are they present at all? You should find at least
cache-control: no-cache, no-store, must-revalidate
header.– Teemu
Nov 15 at 11:58
1
Well you should create a function to read the json data and whenever you are making post request to save data in json , just call that fucntion on sucess of save post , that should solve your issue
– siddhant sankhe
Nov 15 at 12:04
Since I have absolutely no idea what a response header is, I'd say they are not present. I did check the network tab, and there was nothing there. But I do know that accessing the JSON values through Flask (with open, etc) and Jinja2 updates normally.
– Jertyu
Nov 15 at 12:04
1
Headers are small pieces of information your server sends to browser as a part of the response to a HTTPRequest. Open the tab, make the AJAX call run on you page, it should appear in the list on the Network tab (Activate XHR-filter if needed) Then click the AJAX row, and see the headers on the right side of the tab.
– Teemu
Nov 15 at 12:07
"Since I have absolutely no idea what a response header is, I'd say they are not present. "...that's an interesting kind of logic. How can you assert that something is not there when you don't know how to identify it? If someone asked you if there was anything wooden in the room, and you didn't know what wood was, would you say "no, there's none here", or would you say "I need to find about about wood first before I answer"
– ADyson
Nov 15 at 12:39