gpt4 book ai didi

ajax - 如何使 Flask/保持 Ajax HTTP 连接处于事件状态?

转载 作者:行者123 更新时间:2023-12-03 15:28:24 25 4
gpt4 key购买 nike

我有一个 jQuery Ajax 调用,如下所示:

    $("#tags").keyup(function(event) {
$.ajax({url: "/terms",
type: "POST",
contentType: "application/json",
data: JSON.stringify({"prefix": $("#tags").val() }),
dataType: "json",
success: function(response) { display_terms(response.terms); },
});

我有一个像这样的 Flask 方法:
@app.route("/terms", methods=["POST"])
def terms_by_prefix():
req = flask.request.json
tlist = terms.find_by_prefix(req["prefix"])
return flask.jsonify({'terms': tlist})

tcpdump 显示 HTTP 对话框:
POST /terms HTTP/1.1
Host: 127.0.0.1:5000
User-Agent: Mozilla/5.0 (X11; Linux i686; rv:12.0) Gecko/20100101 Firefox/12.0
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Type: application/json; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://127.0.0.1:5000/
Content-Length: 27
Pragma: no-cache
Cache-Control: no-cache

{"prefix":"foo"}

但是,Flask 回复时没有保持事件状态。
HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 445
Server: Werkzeug/0.8.3 Python/2.7.2+
Date: Wed, 09 May 2012 17:55:04 GMT

{"terms": [...]}

难道真的没有实现keep-alive吗?

最佳答案

默认的 request_handler 是 WSGIRequestHandler。

之前 app.run() , 添加一行,WSGIRequestHandler.protocol_version = "HTTP/1.1"
不要忘记from werkzeug.serving import WSGIRequestHandler .

关于ajax - 如何使 Flask/保持 Ajax HTTP 连接处于事件状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10523879/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com