所以我在这里创建了一个ajax请求
function postForm(url, data) {
const xhr = new XMLHttpRequest();
xhr.open('POST', url, true);
xhr.send(data);
xhr.onreadystatechange = function () {
const DONE = 4; // readyState 4 means the request is done.
const OK = 200; // status 200 is a successful return.
if (xhr.readyState === DONE) {
if (xhr.status === OK) {
const requestMessage = xhr.responseText;
console.log(requestMessage); // 'This is the returned text.'
const success = requestMessage.search(1);
const error = requestMessage.search(0);
if(success !== -1) {
console.log("success");
removeClass(modal, 'is-active');
closeModal();
window.location.reload();
} else {
console.log("error");
const parent = findAncestor(this, 'folder');
const errorMessage = document.createElement('div');
errorMessage.className = "errorMessage";
errorMessage.innerHTML = requestMessage;
errorMessage.innerHTML = requestMessage.replace('0','');
parent.append(errorMessage);
};
} else if (xhr.status === INT_ERROR) {
console.log(xhr.responseText);
} else {
console.log('Error: ' + xhr.status); // An error occurred during the request.
}
}
};
}
我对此部分有疑问。我希望能够概括这一点。当用户按下调用此ajax调用的按钮时,ajax会调用他们单击的内容的父级并找到祖先,并将errorMessage添加到该元素的父级。我如何概括这个ajax?
console.log("error");
const parent = findAncestor(this, 'folder');
const errorMessage = document.createElement('div');
errorMessage.className = "errorMessage";
errorMessage.innerHTML = requestMessage;
errorMessage.innerHTML =
requestMessage.replace('0','');
parent.append(errorMessage);