Javascript 在客户端发布数据后未调用Flask render_template()
我有一个带有index.html的flask应用程序,它有一个运行进程的提交输入。我试图捕获错误跟踪路由,然后导航到一个错误页面,以便在发生错误时显示进程中的跟踪路由。但是在服务器上,没有调用我的Javascript 在客户端发布数据后未调用Flask render_template(),javascript,python,flask,fetch,Javascript,Python,Flask,Fetch,我有一个带有index.html的flask应用程序,它有一个运行进程的提交输入。我试图捕获错误跟踪路由,然后导航到一个错误页面,以便在发生错误时显示进程中的跟踪路由。但是在服务器上,没有调用我的render\u template() index.html,它有一个提交输入,通过重定向到b.html来运行进程,在b.html中,使用Fetch api的方法获取映射路径/run,进程运行。该过程运行正常,但如果我为测试插入一个错误,就会抛出一个错误,并将以下JSON对象发送到b.html: {'r
render\u template()
index.html,它有一个提交输入,通过重定向到b.html来运行进程,在b.html中,使用Fetch api的方法获取映射路径/run
,进程运行。该过程运行正常,但如果我为测试插入一个错误,就会抛出一个错误,并将以下JSON对象发送到b.html:
{'run_result':'run_failure' , 'error_output':output}
上面,如果returncode不为零,则输出是subprocess.run()的stdout=PIPE
在b.html中,我有以下内容:
const nav = async() => {
const response = await fetch('run');
if( response.status !== 200 ) {
throw new Error('error message');
}
return await response.json();
};
document.addEventListener('DOMContentLoaded', () => {
nav().then(data => {
if(data != null) {
if(typeof(data) === 'object') {
if(data['run_result'] === 'run_success'){
// ...successful run <== works fine!
} else {
var error_output = data['error_output'];
var sent_data = { 'data': error_output};
fetch(`/run_error`, { // posts data to server uri
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(sent_data)
})
// ***I used to have '=> response.json()' here, but I got SyntaxError: "JSON.parse: unexpected character at line 1 column 1..."
.then(response => response)
// ***console only displays 'success:' here, not sent_data value
.then(console.log('Success:', sent_data));
}
}
}
});
});
为什么不调用render_template()
4/20/21响应
由于在运行时出现语法错误,我无法按照下面的建议执行操作
.then(
console.log('Success:', sent_data);
var response = JSON.parse(response);
var error_output = response["error_output"];
window.location.href = "display_error/"+error_output;
);
…但我现在就在这里(注释掉的then()语句将正确的值打印到控制台):
^^^回溯没有引号。应该吗
服务器控制台显示:
127.0.0.1 - - [date] "GET /display_error/Traceback...." 404
建议?这是因为您在处理POST请求时返回了模板。您实际上是将模板解析为对客户端的响应,而不是要呈现的页面。相反,我建议您重定向到另一个视图函数,用来自客户端的GET请求显示错误,您将
error\u输出作为查询字符串的值传递给该请求。如果需要事先对输出运行某些逻辑,可以保留原始视图功能error
。否则,您只需使用新的display\u error
,而无需查看第一个
@app.route('/run\u error/',methods=['POST'])
def error():
如果request.method='POST':
request_json=request.json
错误\u输出=请求\u json['data']
返回json.dumps({'error\u output':error\u output})
@app.route('/display\u error/',methods=['GET'])
def显示错误()
error\u output=request.args.get('error\u output')
返回渲染模板('run\u error.html',error\u output=error\u output)
然后,在post请求成功(或没有post请求)后,客户端将重定向到display\u error
view函数,并将error\u输出作为参数传递:
.then(response => response)
.then(
console.log('Success:', sent_data);
var response = JSON.parse(response);
var error_output = response["error_output"];
window.location.href = "/display_error/?error_output="+error_output;
);
最后一部分中的“状态”来自何处?您的成功(响应)片段能否在my.then(响应=>response)中解析?对不起,它应该说error\u output
。只是修改了代码片段,这样就可以在代码中使用了。我接受了答案,尽管我们还没有完全解决它。我想我们很接近了!最后(!)回到这个问题上,它成功了!!再次感谢。
127.0.0.1:5000/display_error/Traceback....
127.0.0.1 - - [date] "GET /display_error/Traceback...." 404
.then(response => response)
.then(
console.log('Success:', sent_data);
var response = JSON.parse(response);
var error_output = response["error_output"];
window.location.href = "/display_error/?error_output="+error_output;
);