Flask文件在Javascript获取时未更新

Flask文件在Javascript获取时未更新,javascript,python,flask,Javascript,Python,Flask,我有一个简单的烧瓶服务器。用户首先将音乐文件上传到我的服务器(mp3),我的服务器处理该文件,创建一个新的结果文件(MusicXML),然后我在浏览器上渲染该文件 以下是我的建议: @app.route('/', methods=['GET', 'POST']) def index(): if request.method == "GET": return render_template('index.html', request="GET") else: file =

我有一个简单的烧瓶服务器。用户首先将音乐文件上传到我的服务器(mp3),我的服务器处理该文件,创建一个新的结果文件(MusicXML),然后我在浏览器上渲染该文件

以下是我的建议:

@app.route('/', methods=['GET', 'POST'])
def index():
  if request.method == "GET":
    return render_template('index.html', request="GET")
  else:
    file = request.files['file']
    handle_file(file)
    return render_template('index.html', request="POST")

@app.route('/mxl')
def mxl():
  return send_from_directory(UPLOAD_FOLDER, 'piece.mxl')

def handle_file(file):
  filename = secure_filename(file.filename)
  filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
  file.save(filepath)
  pitches = mir.transcribe(filepath)
  os.remove(filepath)
所以这里我们可以看到,首先,用户访问
'/'
。然后上传音乐文件(
POST
method),并调用
handle\u file()
。这将调用
mir.transcribe
,后者处理该文件并创建一个“结果”musicXML文件

MusicXML文件是使用Music21模块创建并存储在静态文件夹中的。因此,在包
mir
中,我们有:

def transcribe(filename):
  ...
  note_stream.write("musicxml", "static/piece.mxl")
handle\u file()
返回时,我们使用
request='POST'
调用
render\u template
。这是我的
index.html

{%- extends "base.html" %}

{% import "bootstrap/utils.html" as utils %}

{% block content %}
  {% if request == "GET": %}

     <!-- uploading file form -->  

  {% else: %}

  <script>
    // This is the important part, where we call fetch to obtain the
    // MusicXML file we created on the server.
    fetch('http://localhost:5000/mxl')
      .then(function (response) {
        return response.text();
      })
      .then(function (mxl) {
        console.log(mxl);
        return embed.loadMusicXML(mxl);
      })
      .then(function () {
        console.log("Score loaded in the embed!");
      })
      .catch(function (error) {
        console.log("Unable to load the score!");
      });
  </script>

  {% endif %}
{% endblock %}

{% block scripts %}
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript"></script>
  <!--<script src="static/js/record.js"></script>-->
{% endblock %}
{%-扩展“base.html”%}
{%import“bootstrap/utils.html”作为utils%}
{%block content%}
{%if请求==“GET”:%}
{%else:%}
//这是重要的部分,在这里我们调用fetch来获取
//我们在服务器上创建的MusicXML文件。
取('http://localhost:5000/mxl')
.然后(功能(响应){
返回response.text();
})
.then(函数(mxl){
控制台日志(mxl);
返回embed.loadMusicXML(mxl);
})
.然后(函数(){
log(“嵌入中加载的分数!”);
})
.catch(函数(错误){
log(“无法加载分数!”);
});
{%endif%}
{%endblock%}
{%block scripts%}
{%endblock%}
因此,正如您所看到的,在我们
render_template
之后,
fetch
被调用

问题是,fetch有时不返回新创建的MusicXML文件,而是返回它的早期版本。但是,如果我转到我的
static
文件夹,其中包含的MusicXML文件就是新的


为什么会发生这种情况?

如果需要,可以强制禁用所有请求的缓存,例如:

@app.after_request
def add_header(response):
    response.cache_control.max_age = 300
    if 'Cache-Control' not in response.headers:
        response.headers['Cache-Control'] = 'no-store'
    return response
或者,您可以为
应用程序中的所有静态文件设置默认值:

app = Flask(__name___)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 60

您是否检查了您的响应头(特别是
缓存控制
)?这实际上将我引向了问题所在,我认为(不确定)问题只在我打开devtools时存在,因为我勾选了“在devtools打开时禁用缓存”。因此,当我的devtools打开时,头中没有缓存,而当devtools没有时,我没有缓存。我怎么做才能使fetch语句始终没有缓存?那么我不必返回响应吗?不使用
send\u from\u directory
?我用我的代码替换了该代码,没有任何更改<响应头中的代码>缓存控制
仍然是公共的,最大年龄=43200。啊,天啊,对不起,我的错误,马上更新答案!