我在
mdn web docs上阅读了有关
send
方法的 XMLHttpRequest
。
我无法理解“使用事件传递结果是什么意思”
如果请求是异步的(默认情况),则该方法会在请求发送后立即返回,并使用事件传递结果。如果请求是同步的,则在响应到达之前此方法不会返回。
MDN 文档引用了这些事件:https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest#events,特别是
onload
和 onreadystatechange
事件。
来自他们的 GET 示例:
const xhr = new XMLHttpRequest();
xhr.open('GET', '/server', true);
// "onload" is the event and executes the arrow function when triggered.
xhr.onload = () => {
// Request finished. Do processing here.
};
xhr.send(null);
这意味着您必须将适当的
event listener
添加到 XMLHttpRequest
对象,该对象将在进程完成时调度事件。