M
M
Michael2016-10-03 11:25:30
JavaScript
Michael, 2016-10-03 11:25:30

Why does the response from the server come 3 times?

On the client, ajax is generated on pure js (without jQuery). On the server I process on express.js

app.use(bodyParser.json());
app.post('/name', (req, res) => {
  res.status(200);
});


I create a request on the client:
form.addEventListener('submit', event => {
      event.preventDefault();
      var xhr = new XMLHttpRequest();
      var json = JSON.stringify({
              name: document.querySelector('#name-reg').value,
              pass: document.querySelector('#pass-reg').value,
            });
      xhr.open('POST', '/name', true);
      xhr.setRequestHeader("Content-Type", "application/json");
      xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
      xhr.send(json);

      xhr.onreadystatechange = () => {
        xhr.status === 200 ? console.log('Зарегистрирован') : console.log('не зарегистрирован');
      };
    });

And Everything works, but the response comes to the client 3 times. At the same time, everything is empty in the consoles, that is, no errors occur.

UPD: I will clarify that there is only one request in the network tab, but the message is displayed in the console three times.
What is the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
Zakharov Alexander, 2016-10-03
@mak_ufo

Because somewhere in the code you request it three times. See not only the browser console, but also the network connections tab. Better yet, install fiddler and check it out.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question