简体   繁体   English

发送不改变用户当前页面的 HTTP 响应

[英]Send HTTP response that doesn't change the user's current page

I have a JavaScript bookmarklet that POSTs information to a (Flask powered) server while the user is on some other page (ie not one on my server).我有一个 JavaScript 书签,当用户在其他页面(即不在我的服务器上)时,它会将信息发布到(由 Flask 驱动的)服务器。 I don't want to interrupt the user's browsing by hijacking their session with my server response.我不想通过用我的服务器响应劫持他们的会话来中断用户的浏览。

My initial thought was that I could suppress the HTTP response from Flask somehow;我最初的想法是我可以以某种方式抑制来自 Flask 的 HTTP 响应; prevent it from sending anything to the client so they aren't mysteriously redirected.防止它向客户端发送任何内容,这样它们就不会被神秘地重定向。 I was hoping I could do this by perhaps having a null return from a view.我希望我可以通过从视图中获得空返回来做到这一点。

I then thought that might be some HTTP response that lets the client know the information was successfully submitted, but will leave the client on their current page.然后我认为这可能是一些 HTTP 响应,让客户端知道信息已成功提交,但会将客户端留在其当前页面上。 Suppose a header value like "Here is the result of your request, but you should not alter your current display"?假设标题值类似于“这是您请求的结果,但您不应更改当前显示”?

To answer your amended question, yes there is such a response.要回答您修改后的问题,是的,有这样的答复。 From RFC 2616-section 10 (emphasis added):来自RFC 2616-section 10 (强调):

10.2.5 204 No Content 10.2.5 204 无内容

The server has fulfilled the request but does not need to return an entity-body, and might want to return updated metainformation.服务器已完成请求但不需要返回实体主体,并且可能想要返回更新的元信息。 The response MAY include new or updated metainformation in the form of entity-headers, which if present SHOULD be associated with the requested variant.响应可以包含实体头形式的新的或更新的元信息,如果存在,应该与请求的变体相关联。

If the client is a user agent, it SHOULD NOT change its document view from that which caused the request to be sent .如果客户端是一个用户代理,它不应该改变导致请求被发送的文档视图 This response is primarily intended to allow input for actions to take place without causing a change to the user agent's active document view, although any new or updated metainformation SHOULD be applied to the document currently in the user agent's active view.此响应主要是为了允许输入操作,而不会导致用户代理的活动文档视图发生变化,尽管任何新的或更新的元信息都应该应用于当前在用户代理的活动视图中的文档。

The 204 response MUST NOT include a message-body, and thus is always terminated by the first empty line after the header fields. 204 响应不能包含消息体,因此总是由头字段之后的第一个空行终止。

Thus from flask you can do something like this.因此从烧瓶你可以做这样的事情。 Remember, the response must not include a message body, so any data you want to send back should be put into a cookie.请记住,响应不得包含消息正文,因此您要发回的任何数据都应放入 cookie。

@app.route('/')
def index():
    r = flask.Response()
    r.set_cookie("My important cookie", value=some_cool_value)
    return r, 204

No, it is not possible.不,这是不可能的。 Flask is built on Werkzeug, which implements the WSGI spec. Flask 建立在 Werkzeug 之上,它实现了 WSGI 规范。 The WSGI cycle requires sending a response to each request. WSGI 循环需要对每个请求发送响应。 Droping the response would require control over the TCP/IP connection at a far lower level even that HTTP.丢弃响应需要在低得多的级别甚至是 HTTP 控制 TCP/IP 连接。 This is outside the domain of WSGI, therefore outside the domain of Flask.这在 WSGI 的领域之外,因此在 Flask 的领域之外。

You could return an error code, or an empty body, but you have to return something .您可以返回错误代码或空正文,但您必须返回某些内容

return ''  # empty body

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

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