最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

xmlhttprequest - Use Http2 to make api calls from javascript - Stack Overflow

programmeradmin6浏览0评论

I know most browsers support http/2 for loading the pages but does this mean I can leverage it when I make api calls using XmlHttpRequest ?

More specifically my question is if I make 2 calls to fetch data using XmlHttpRequest does it ensure that both of them use the same tcp connection underneath ? None of the documentation I read any where specifies any thing about http2 support for XmlHttpRequest or how I can explicitly open a http2 connection, make some calls leveraging this and then close the connection.

The okHttp, jetty and other libraries in java offer client libraries to support this. But the javascript support is not clear.

I know most browsers support http/2 for loading the pages but does this mean I can leverage it when I make api calls using XmlHttpRequest ?

More specifically my question is if I make 2 calls to fetch data using XmlHttpRequest does it ensure that both of them use the same tcp connection underneath ? None of the documentation I read any where specifies any thing about http2 support for XmlHttpRequest or how I can explicitly open a http2 connection, make some calls leveraging this and then close the connection.

The okHttp, jetty and other libraries in java offer client libraries to support this. But the javascript support is not clear.

Share Improve this question asked Aug 12, 2016 at 15:38 Ravi Teja PidaparthiRavi Teja Pidaparthi 1231 gold badge1 silver badge5 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 16

When the browser and server both support HTTP/2 then it will be used for all calls from the browser - including XHR. You don't need to do anything special in your javascript to enable this. That's one of the great things about the way HTTP/2 was implemented.

If you are asking how to ensure the connection is held open for as you want to reuse the connection then the http/2 spec says:

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.

发布评论

评论列表(0)

  1. 暂无评论