【发布时间】:2022-01-10 21:49:39
【问题描述】:
我正在尝试使用来自 Ajax 的变量为 Jinja 模板创建图像标签。
success: function (response) {
var pythonResponse = "<ul id='userList'>";
$.each(response,function(index,value) {
console.log(`path is ${value[0]}`)
pythonResponse += `<li onClick = 'selectUser("${value[1]}")'> ${value[1]}`
pythonResponse += `<img src={{ url_for('static', filename='${value[0])}')}}/></li>`
console.log(pythonResponse);
});
有问题的行是这一行
pythonResponse += `<img src={{ url_for('static', filename='${value[0])}')}}/></li>`
当我运行此代码时,包含文件路径的变量 value[0] 被渲染为一堆 gobbledygook。这看起来像%24%7Bvalue%5B0%5D%29%7D/,但它应该看起来像这样/myfolder/myFile.jpg。我已经确认该变量确实等于它应该等于的值,如果我将文件路径硬编码到filename='' 中,那么它就可以工作。
为什么这个变量会从这个/myfolder/myFile.jpg变成这个%24%7Bvalue%5B0%5D%29%7D/?
【问题讨论】:
标签: javascript python ajax flask url-for