Javascript render_模板未呈现POST请求中指定的模板
在index.html中,我得到一个存储在localStorage中的值: 但是,未呈现channel.html。相反,它保持在同一页上。我做错了什么?如果需要,我可以提供更多详细信息。您需要定义request.onload函数。收到响应后将调用此函数。因此,您的JS代码将类似于此:Javascript render_模板未呈现POST请求中指定的模板,javascript,ajax,flask,Javascript,Ajax,Flask,在index.html中,我得到一个存储在localStorage中的值: 但是,未呈现channel.html。相反,它保持在同一页上。我做错了什么?如果需要,我可以提供更多详细信息。您需要定义request.onload函数。收到响应后将调用此函数。因此,您的JS代码将类似于此: function checkChannel() { if (localStorage.getItem('channel')) { const channel = localStorage.getIt
function checkChannel() {
if (localStorage.getItem('channel')) {
const channel = localStorage.getItem('channel');
const request = new XMLHttpRequest();
request.open('POST', '/convert');
// Add data to send with request
const data = new FormData();
data.append('channel', channel);
// Send request
request.send(data);
// 4. This will be called after the response is received
request.onload = function() {
if (request.status != 200) {
// analyze HTTP status of the response
alert(`Error ${request.status}: ${request.statusText}`);
// e.g. 404: Not Found
} else { // show the result
$('body').html(request.response)
}
};
}
}
请注意,您可以根据channel.html文件内容将$'body'.html'some content'替换为$'div'.html'some content'或$'some-id'.html'some content'
请查找。可以,但新页面上的DOMContentLoaded事件未触发。请尝试使用$body而不是$body。请找到我的编辑。实际上,我是这样做的:document.body.innerHTML=request.responseeyes,有许多解决方案可以替换正文内容。
@app.route("/convert", methods=["POST"])
def convert():
channel = request.form.get("channel")
channel_messages = channels_dict[channel]
return render_template("channel.html", channel=channel, channel_messages=channel_messages)
function checkChannel() {
if (localStorage.getItem('channel')) {
const channel = localStorage.getItem('channel');
const request = new XMLHttpRequest();
request.open('POST', '/convert');
// Add data to send with request
const data = new FormData();
data.append('channel', channel);
// Send request
request.send(data);
// 4. This will be called after the response is received
request.onload = function() {
if (request.status != 200) {
// analyze HTTP status of the response
alert(`Error ${request.status}: ${request.statusText}`);
// e.g. 404: Not Found
} else { // show the result
$('body').html(request.response)
}
};
}
}