Does the XMLHttpRequest object close after receiving a response?

I am trying to connect to the server using an XMLHttpRequest object to publish data at different times. I create an object and "connect" to the server like this:

var xhr = new XMLHttpRequest(); xhr.open("post", location, true); xhr.send(); //Is this send call needed to open the connection? 

And at a later point, I call something like this:

 xhr.send("Something to send"); 

However, looking at the developer's console, it seems that only the initial request has passed (and successfully answered). The second request does not seem to be sent. I am trying to narrow down what could be the problem, so I thought: can the connection be closed after receiving a response? Why should he stay open? So my question is: is the XMLHttpRequest object connection closed after receiving the response? If so, what is the best way to simulate a continuously open connection (to constantly connect?)?

+6
source share
1 answer

Yes, if you have not tricked your server to save it in memory, it will be closed after the response is sent.

You might want to search for websites. But if you do not want to play with them, just create a new HttpRequest for each of your "requests".

Basic HTTP connection: 1 request → 1 response → closed!

Edit: keep in mind that websockets is a new feature from HTML5, so it will not work for all browsers, and if they work for some browsers, they may not be fully implemented.

+4
source

Source: https://habr.com/ru/post/981421/


All Articles