for(var i =0; i < 10; i++) {
var user_zapros = JSON.stringify({user: i});
var request = new XMLHttpRequest();
request.open("POST", "/user", true);
request.setRequestHeader("Content-Type", "application/json");
request.addEventListener("load", function () {
var result = JSON.parse(request.response);
})
request.send(user_zapros);
}
Uncaught SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
const results = [];
const promises = [];
for (let i = 0; i < 10; i++) {
promises.push(
fetch('/user', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({user: i}),
})
.then(response => response.json())
.then(data => results.push(data))
.catch(err => console.error(err))
);
}
Promise.all(promises)
.then(() => console.log("All 10 done!", results));