Ajax请求答案比代码慢,ajax处理代码后请求
Ajax Request Answer is slower than code, ajax request after code is processed
我收到 ajax 请求非常慢,所以我的统计数据数组在我第二次访问它时没有得到更新。似乎 ajax 请求是在处理完所有代码后执行的。有没有办法让我等待 ajax 请求在我的所有其余代码之前执行?
console.log(stats);
fetchREST(false, (url+selectedID+"/solve"), (parseInt(button.id)+1), function(data){
if (data.success) {
console.log("richtig");
stats.right++;
} else {
console.log("falsch");
stats.wrong++;
}
});
console.log(stats);
function fetchREST(isGET, path, answer, callback) {
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200) {
var data = JSON.parse(httpRequest.responseText);
if (callback) callback(data);
}
}
};
let email = "*";
let pw = "*";
if(isGET) {
// not needed
} else {
let solutionArray = '[' + answer + ']';
console.log(solutionArray);
httpRequest.open('POST', path, true);
httpRequest.setRequestHeader("Content-Type", "application/json");
httpRequest.setRequestHeader("Access-Control-Allow-Origin", "*");
httpRequest.setRequestHeader("Access-Control-Allow-Headers", "*");
httpRequest.setRequestHeader("Authorization", "Basic " + window.btoa(email + ":" + pw));
httpRequest.send(solutionArray);
}
}
编辑
我通过更改
修复了它
httpRequest.open('POST', path, true);
到
httpRequest.open('POST', path, false);
现在异步设置为 false,发送方法等待收到响应。
我通过更改
自己修复了它
httpRequest.open('POST', path, true);
至
httpRequest.open('POST', path, false);
现在异步设置为 false,发送方法等待收到响应。
我收到 ajax 请求非常慢,所以我的统计数据数组在我第二次访问它时没有得到更新。似乎 ajax 请求是在处理完所有代码后执行的。有没有办法让我等待 ajax 请求在我的所有其余代码之前执行?
console.log(stats);
fetchREST(false, (url+selectedID+"/solve"), (parseInt(button.id)+1), function(data){
if (data.success) {
console.log("richtig");
stats.right++;
} else {
console.log("falsch");
stats.wrong++;
}
});
console.log(stats);
function fetchREST(isGET, path, answer, callback) {
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200) {
var data = JSON.parse(httpRequest.responseText);
if (callback) callback(data);
}
}
};
let email = "*";
let pw = "*";
if(isGET) {
// not needed
} else {
let solutionArray = '[' + answer + ']';
console.log(solutionArray);
httpRequest.open('POST', path, true);
httpRequest.setRequestHeader("Content-Type", "application/json");
httpRequest.setRequestHeader("Access-Control-Allow-Origin", "*");
httpRequest.setRequestHeader("Access-Control-Allow-Headers", "*");
httpRequest.setRequestHeader("Authorization", "Basic " + window.btoa(email + ":" + pw));
httpRequest.send(solutionArray);
}
}
编辑 我通过更改
修复了它httpRequest.open('POST', path, true);
到
httpRequest.open('POST', path, false);
现在异步设置为 false,发送方法等待收到响应。
我通过更改
自己修复了它httpRequest.open('POST', path, true);
至
httpRequest.open('POST', path, false);
现在异步设置为 false,发送方法等待收到响应。