javascript - 我想在发布数据后路由到新网页
问题描述
我已经定义了这条路线,虽然 render_template 以前对我有用,但我似乎无法弄清楚为什么网页会停留在现有页面上。我确定它会一直命中并处理,因为我返回了 200,好的,并在 javascript 端收到了。我什至尝试重定向以将请求作为 GET 处理并允许 get 到 render_template,但仍然没有运气。有任何想法吗?
路线代码
@app.route("/view", methods=["GET","POST"])
@cross_origin()
def view_playlist():
global playlist_item
if request.method == 'POST':
playlist_item = request.get_json()
print("playlist POST")
return render_template('/view.html', result= playlist_item)
# return redirect(url_for('view_playlist'))
else:
print("This is a GET")
return render_template('/view.html', result= playlist_item)
# app.send_static_file("index.html", result= playlist_item)
javascript帖子如下
url = local + '/view'
fetch(url,{
headers: {
'Content-Type': 'application/json'
},
method: 'POST',
body:JSON.stringify(itemsPlaylist)
}).then(function(data) {
// location.replace(local + '/view.html');
})
解决方案
看代码想不出任何明显的原因,但值得在那时打印响应并放入一个 catch 块。
您也可以使用 finally 进行路由
fetch(myRequest)
.then(function(response) {})
.catch(function(error) { console.error(error); })
.finally(function() { .. do the routing here });
推荐阅读
- typescript - MaterialTopTabNavigator 给出错误最大深度超出
- ruby-on-rails - Rails类'ActionController :: Metal'上的'Metal'是什么意思
- azure - 在 Kubernetes 中以集群模式设置 Tomcat 引发超时异常
- batch-file - 批处理脚本 | 命令运行时闪烁屏幕
- django - 如何在 Django 中使用唯一但为空的字段保存模型
- prometheus - how to display the rate at which requests are being received over a certain time interval in grafana
- python - 外键约束形成错误——python sqlalchemy
- java - 为什么 Kryo 在序列化对象时给我这个错误?
- lua - love2d lua中的鼠标按下错误
- django - 部分形式在 Django 中是一种好的做法吗?