当我尝试在jQuery.get中使用URL变量时,它不起作用。但是当我对内容进行硬编码时,它就可以正常工作。
javascript:
1 - function readFile() {
2 - var myurl = '{{ fName }}'; <<< jinja variable containing filename
3 - console.log(myurl); <<< prints correct URL in console
4 - jQuery.get(myurl, function(data){
5 - console.log(data); <<< outputs a period (ie ".")
6 - jQuery('#target').html(data);
7 - });
8 - }
如果我将第3行的输出硬编码到第4行的myurl中,则该函数将工作并读取文件并在控制台中显示其内容。
我尝试将单引号,双引号放在您的第4行的myurl周围,但没有用。
@Mike
我正在从python调用模板,如下所示:
def hcheck():
print("hckeck get")
baseName = '/static/' + str(time.time())
createDir(appDir + baseName)
return render_template("hcheck_form.html", fName = baseName+'/healthCheck.txt')
js代码位于hcheck_form.html中。那是你的意思吗?
*更新1 * 我将web.py响应标头(工作)与Flask响应标头进行了比较,我认为这可能是我问题的根本原因。 Web.py返回以下内容:
Web.py
======
Request URL:
https://<url>:8080/static/1565974246.91/healthCheck.txt
Request Method: GET
Status Code: 200
Remote Address: <ip>:8080
Referrer Policy: no-referrer-when-downgrade
Content-Length: 22
Content-type: text/plain
Date: Fri, 16 Aug 2019 16:50:54 GMT
ETag: "1565974253.04"
Last-Modified: Fri, 16 Aug 2019 16:50:53 GMT
Server: localhost
Provisional headers are shown
Accept: */*
Referer: https://<url>:8080/
Sec-Fetch-Mode: cors
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36
X-Requested-With: XMLHttpRequest
Flask
=====
Request URL:
http://<url>:5000/static/1565974312.1353812/healthCheck.txt
Request Method: GET
Status Code: 200 OK
Remote Address: <ip>:5000
Referrer Policy: no-referrer-when-downgrade
Accept-Ranges: bytes
Cache-Control: public, max-age=43200
Content-Length: 1
Content-Type: text/plain; charset=utf-8
Date: Fri, 16 Aug 2019 16:51:58 GMT
ETag: "1565974317.914523-1-875239115"
Expires: Sat, 17 Aug 2019 04:51:58 GMT
Last-Modified: Fri, 16 Aug 2019 16:51:57 GMT
Server: Werkzeug/0.15.4 Python/3.7.1
Provisional headers are shown
Accept: */*
Referer: http://<url>:5000/hcheck?radioId=hcheck
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36
X-Requested-With: XMLHttpRequest
我不是网络专家,我觉得它与Accept-Ranges有关:字节,它不在web.py标头中。不确定。有没有办法修改Flask标头以使其与web.py返回的内容匹配?谢谢
*更新2 * 好吧,这很尴尬。由于我提供的是静态文件,因此该文件正在缓存中,仅显示原始内容,而在函数更新文件时不显示更新内容。如果在函数运行时手动清除缓存,则可以看到更新。不,我只需要弄清楚如何关闭Flask中的缓存,就可以了。谢谢