xcoder
xcoder

Reputation: 11

How to reuse XMLHttpRequest?

let xhr = new XMLHttpRequest();
        let data = JSON.stringify({
            //some data...
        });
        xhr.open('POST', '/users');
        xhr.setRequestHeader("Content-Type", "application/json");
        xhr.send(data);
        let time;
        time = JSON.stringify({
            us: true,
        });
        setInterval(() => {xhr.send(time);}, 5000);

Error: Uncaught DOMException: Failed to execute 'send' on 'XMLHttpRequest': The object's state must be OPENED

Upvotes: 0

Views: 210

Answers (1)

David Federspiel
David Federspiel

Reputation: 341

XMLHttpRequests are closed after the response is sent. I'm unsure of its purpose, but the setInterval is continually attempting to send requests using the same XHR after you make your original request to the users endpoint.

Upvotes: 1

Related Questions