简体   繁体   English

Flask 中的 GET 请求 - 从 python 后端向客户端发送数据

[英]GET request in Flask - send data from python backend to client

I have a page that asks the user for informations, the user selects options and confirms his choice.我有一个页面询问用户信息,用户选择选项并确认他的选择。 The answers are sent to the backend, and a file is made.答案被发送到后端,并生成一个文件。 I want to send back the file to the user but for this I have to send information from the backend to the frontend.我想将文件发回给用户,但为此我必须将信息从后端发送到前端。

I tried using "return render_template" and "redirect" but it didn't work我尝试使用“return render_template”和“redirect”,但没有奏效

I have been told I have to do a GET request but I'm not sure how to do this, I know how to send data from client to server but I never did from server to client and I haven't found how to do this with Flask.有人告诉我我必须做一个 GET 请求,但我不知道该怎么做,我知道如何将数据从客户端发送到服务器,但我从来没有从服务器发送到客户端,我还没有找到如何做到这一点与 Flask。

@app.route('/createdocument', methods=['POST', 'GET'])
#@login_required
def create_document():
    playlists = get_playlists()
    if request.method == "POST":
        request_data = str(request.data.decode('UTF-8'))
        genre = get_header_genre(request_data)
        parsed_data = parse_request(request_data)           
        playlist_names = get_parsed_playlists(parsed_data)
        if genre == "playlist":
            #make_playlist_doc(playlist_names, genre)
            print("playlist option not ready yet")
        elif genre == "socan":
            name = make_socan_doc(playlist_names, genre)
            return render_template("tools/downloadfile.html", document=name)
        else:
            print("other request:")
            print(str(request.data.decode('UTF-8')))
    if request.method == "GET":
        ########################## Something I guess but I don't know what
   return render_template("tools/createdocument.html", playlists=playlists)

I found this looking for a GET request我发现这是在寻找 GET 请求

function httpGetAsync(theUrl, callback)
{
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.onreadystatechange = function() { 
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
            callback(xmlHttp.responseText);
    }
    xmlHttp.open("GET", theUrl, true); 
    xmlHttp.send(null);
}

If I understand well I should but /createdocument in theUrl, but what should I put in callback?如果我理解得很好,我应该在 theUrl 中添加/createdocument ,但是我应该在回调中添加什么?

I think you are on the right track with the return render_template approach.我认为您使用 return render_template 方法走在正确的轨道上。 What exactly isn't working?究竟是什么不工作?

For the client - can you use axios or fetch instead;对于客户端 - 你可以使用axios还是取而代之 those are fairly common libraries these days to get GET requests from the client.如今,这些都是相当常见的库,用于从客户端获取 GET 请求。

axios.get('/createdocument', {
    params: {}
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  })
  .finally(function () {
    // always executed
  });   

That said if you are trying to do all of this asynchronously (ie the user submits the POST request and the file is not returned as part of that request, but is returned at some arbitrary later time), look into something like setting up a web socket也就是说,如果您尝试异步执行所有这些操作(即,用户提交 POST 请求并且文件不会作为该请求的一部分返回,而是在稍后的某个任意时间返回),请查看设置web 之类的东西插座

Here is a Flask-based websocket server that should work这是一个基于 Flask 的 websocket 服务器应该可以工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM