[英]loading html files with jQuery load() not working when delivered with flask
I've seen similar threads on StackOverflow but it doesn't seem to work.我在 StackOverflow 上看到过类似的线程,但它似乎不起作用。 I'm building a simple webapp where the user uploads a docx file and a python script does some processing and returns mp3 files for the user to download.
我正在构建一个简单的 web 应用程序,用户在其中上传 docx 文件,并且 python 脚本进行一些处理并返回 mp3 文件供用户下载。
My script also generates a bunch of HTML files (contents of the doc) that I want to display on my webpage, eg loading text1.html into index.html.我的脚本还生成了一堆 HTML 文件(文档的内容),我想在我的网页上显示这些文件,例如将 text1.html 加载到 index.html 中。 But I'm running into two problems:
但是我遇到了两个问题:
index.html:索引.html:
<html>
<head>
<script
src="https://code.jquery.com/jquery-3.6.0.js"
integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk="
crossorigin="anonymous"></script>
<script>
$(document).ready(function(){
$("#includedContent").load("text1.html");
});
</script>
</head>
<body>
<h3>Heading</h3>
<form method=post enctype=multipart/form-data id="form1">
<input type=file name=uploadfield1>
<!-- <input id="ub" name=uploadbutton1 type=submit value=Upload >-->
<button type="submit" name="uploadbutton1" value=Upload>Upload </button>
<button type="button" name="removebutton" value="Remove">Remove </button>
</form>
<form method ="post" enctype=multipart/form-data >
<button type="submit" name="tag1" value="tag2">Process</button>
</form>
<p>
text selected:
</p>
<div id="includedContent"></div>
</body>
</html>
and text1.html和文本1.html
<p> sample text from text1.html that is in projectfolder/templates folder </p>
I tried changing $("#includedContent").load("text1.html");
我尝试更改
$("#includedContent").load("text1.html");
to $("#includedContent").load("/Users/user/PycharmProjects/project/static/text1.html");
到
$("#includedContent").load("/Users/user/PycharmProjects/project/static/text1.html");
but that didn't work as well..但这也没有用..
so what is the correct way of loading in external HTML files?那么加载外部 HTML 文件的正确方法是什么? Furthermore, since the files I'll be loading will be generated after
index.html
is loaded in, is there a better way to load these files?此外,由于我要加载的文件将在
index.html
加载后生成,有没有更好的方法来加载这些文件? As I'm assuming things inside the static folder should be, well, static.正如我假设 static 文件夹中的内容应该是 static。
You can use ajax for it, it will not reload the page and can get the response.您可以使用 ajax ,它不会重新加载页面并且可以获得响应。
$.ajax({
type: "GET",// method
url: `${window.location.pathname}`, // route
data: {
"key":"value" // json format in key value pair
},
contentType: "application/json;charset=UTF-8",
success: function (res) {
console.log(res); // contents of text1.html
}
});
You can use request.form.get("key") # key for the value
and then return the text1.html您可以使用
request.form.get("key") # key for the value
,然后返回 text1.html
return render_template("text1.html")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.