gpt4 book ai didi

javascript - 使用 Http/2 从 javascript 调用 api

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:00:50 25 4
gpt4 key购买 nike

我知道大多数浏览器都支持 http/2 来加载页面,但这是否意味着我可以在使用 XmlHttpRequest 进行 api 调用时利用它?

更具体地说,我的问题是,如果我使用 XmlHttpRequest 进行 2 次调用以获取数据,它是否确保它们都在下面使用相同的 tcp 连接?我阅读的任何文档都没有具体说明有关对 XmlHttpRequest 的 http2 支持或我如何显式打开 http2 连接、利用它进行一些调用然后关闭连接的任何事情。

java中的okHttp、jetty等库提供了客户端库来支持这一点。但是javascript支持不清楚。

最佳答案

当浏览器和服务器都支持 HTTP/2 时,它将用于来自浏览器的所有调用——包括 XHR。你不需要在你的 javascript 中做任何特殊的事情来启用它。这是 HTTP/2 实现方式的一大优点。

如果您询问如何确保连接保持打开状态,因为您想要重用连接,那么 http/2 spec说:

HTTP/2 connections are persistent. For best performance, it is expected that clients will not close connections until it is determined that no further communication with a server is necessary (for example, when a user navigates away from a particular web page) or until the server closes the connection.

...

Servers are encouraged to maintain open connections for as long as possible but are permitted to terminate idle connections if necessary.

关于javascript - 使用 Http/2 从 javascript 调用 api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38921726/

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