Javascript 加载数据后,我如何才能取回XMLHttpRequest?
我有一个代码,其中“onloadend”在“get”任务之后运行,如果先运行“get”,代码将以正确的方式工作。所以我需要一种方法来切换它们。我必须使用HttpClient和JavaScript,我不能使用jQuery或其他任何东西。 以下是我在CodePen中的完整代码: 以下是onloadend:Javascript 加载数据后,我如何才能取回XMLHttpRequest?,javascript,get,xmlhttprequest,httpclient,Javascript,Get,Xmlhttprequest,Httpclient,我有一个代码,其中“onloadend”在“get”任务之后运行,如果先运行“get”,代码将以正确的方式工作。所以我需要一种方法来切换它们。我必须使用HttpClient和JavaScript,我不能使用jQuery或其他任何东西。 以下是我在CodePen中的完整代码: 以下是onloadend: export class HttpClient { constructor(url) { this.url = url; this.xhr = new XM
export class HttpClient {
constructor(url) {
this.url = url;
this.xhr = new XMLHttpRequest();
this.xhr.onloadend = (event) => {
return this.xhr.result;
};
}
我在这里设置标题:
setHeader() {
this.xhr.setRequestHeader('Content-Type','application/json');
}
下面是get函数:
get(async, header) {
return new Promise((resolve, reject) => {
this.xhr.open('GET', this.url, async);
this.xhr.setRequestHeader(header.name, header.value);
this.xhr.send();
resolve(this.xhr.response);`enter code here`
});
}
这是我邀请的函数get函数
getAll() {
this.httpClient.get(true, this.header).then((result) => {
const data = !!result ? result : '[]';
const items = JSON.parse(data);
console.log('result:', result);
items.forEach(item => {
const todo = new Todo(item.id, item.name, item.status);
this.items.push(todo);
});
});
}
如何解决有关任务顺序的问题?1。请准确地写出如何声明get()*和**getAll()函数,例如var getAll=function(){}。2.添加一个输入示例,这里所需的输出是我的完整代码@besciualex