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"










share|improve this question
























  • 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

















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"










share|improve this question
























  • 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















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"










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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




















  • 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


















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














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) {





share|improve this answer





















  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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) {





share|improve this answer





















  • 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















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) {





share|improve this answer





















  • 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













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) {





share|improve this answer












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) {






share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?