如何让 JavaScript fetch() 函数更新到添加到文件的新数据

How do you get the JavaScript fetch() function to update to the new data being added to the file

我有一个 JSON 文件,其中包含一些我想写入屏幕的数据。我有一个包含 fetch() 请求的函数,每次我单击一个按钮时 运行s。这在我第一次单击按钮时效果很好,因为它获取数据并正确输出。但是一旦 JSON 文件添加了新内容并且我在同一个会话中第二次按下同一个按钮,所有数据都与第一次 运行 完全相同。而且我知道 fetch() 之后的代码是 运行(console.log(data.lenght);)。我已经阅读了很多关于 Fetch API 的文章,但我一直没能找到帮助解决这个问题的东西。我还在另一个线程上读到,fetch() 函数仅 运行s 一次,但代码总是在 运行s 之后,似乎证实了我的问题。

function fetchJSON() {

    fetch("static/json/pool_values.json")
        .then(response => response.json())
        .then(data => {
            console.log(data.length);

您的缓存似乎有问题。 这可以通过向您的请求传递一个选项来解决。

fetch("static/json/pool_values.json", {cache: 'no-store'})
        .then(response => response.json())
        .then(data => {
            console.log(data.length);