首页 > 解决方案 > 我想在发布数据后路由到新网页

问题描述

我已经定义了这条路线,虽然 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');
    })

标签: javascriptpythonflaskpost

解决方案


看代码想不出任何明显的原因,但值得在那时打印响应并放入一个 catch 块。

您也可以使用 finally 进行路由

fetch(myRequest)
.then(function(response) {})
.catch(function(error) { console.error(error); })
.finally(function() { .. do the routing here });

推荐阅读