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

javascript - Does XMLHttpRequest object close after response received? - Stack Overflow

programmeradmin3浏览0评论

I'm trying to connect to the server using an XMLHttpRequest object to post data at different times. I create an object and "connect" to the server like so:

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 in time, I call something like this:

xhr.send("Something to send");

However, looking at the developer console, it seems that only the initial request went through (and successfully responded). The second request doesn't seem to send. I'm trying to narrow down what could be the problem, so I thought: could the connection be closed once the response is received; Why would it be kept open? So, my question: Is the XMLHttpRequest object connection closed once it receives a response? If so, what's the best way to simulate a continuously open connection (to constantly reconnect?)?

I'm trying to connect to the server using an XMLHttpRequest object to post data at different times. I create an object and "connect" to the server like so:

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 in time, I call something like this:

xhr.send("Something to send");

However, looking at the developer console, it seems that only the initial request went through (and successfully responded). The second request doesn't seem to send. I'm trying to narrow down what could be the problem, so I thought: could the connection be closed once the response is received; Why would it be kept open? So, my question: Is the XMLHttpRequest object connection closed once it receives a response? If so, what's the best way to simulate a continuously open connection (to constantly reconnect?)?

Share Improve this question asked Jan 22, 2015 at 21:41 chRyNaNchRyNaN 3,6925 gold badges49 silver badges74 bronze badges 5
  • 1 You need to create a new request object for an other request. You should not call send multiple times on the same object. – Bergi Commented Jan 23, 2015 at 0:20
  • @Bergi Wouldn't it be more practical to just re-call the open method on the object? Or will that not work? – chRyNaN Commented Jan 23, 2015 at 0:57
  • 1 I don't see how that would be "more practical". What do you think you gained by reusing the object? IIRC, some older browsers had a few bugs with that. – Bergi Commented Jan 23, 2015 at 2:46
  • 1 Well, though not dramatic at all in this case, you save garbage collection time for one. If it is a once use then throw away object, that is just poor design. . – chRyNaN Commented Jan 23, 2015 at 16:20
  • 2 "not dramatic" might even be a bit overstated :-) These objects are fairly low-cost (and seldomly needed), so I'd really go for simplicity first. – Bergi Commented Jan 23, 2015 at 22:47
Add a ment  | 

1 Answer 1

Reset to default 8

Yes, if you haven't tricked your server to keep it alive, it will be closed after the response is sent.

Maybe you want to look for websockets. But if you don't want to play with those, just create a new HttpRequest for each of your "request".

Basic munication with HTTP: 1 request -> 1 response -> closed!

Edit: Keep in mind that websockets is a new feature from HTML5 so it won't work for every browsers and if they work for some browsers, they maybe are not pletely implemented.

发布评论

评论列表(0)

  1. 暂无评论