AJAX - 使用 XMLHttpRequest 的命令顺序(新手)
在我在互联网上找到的大多数示例中,我看到这样的内容:
ajaxRequest.onreadystatechange = function() {
if(ajaxRequest.readyState == 4) {
document.myForm.time.value = ajaxRequest.responseText;
}
}
ajaxRequest.open("GET", "serverTime.php", true);
ajaxRequest.send(null);
当“更改状态”时,这怎么可能以及此代码如何工作 属性已检查 之前 执行打开和发送命令? 我知道它有效......但是流程如何返回到“检查状态” 执行“打开”和“发送”后的“状态”。
感激任何帮助,非常感谢:-)
我将不胜
In most examples I found on the Internet , I see something like this :
ajaxRequest.onreadystatechange = function() {
if(ajaxRequest.readyState == 4) {
document.myForm.time.value = ajaxRequest.responseText;
}
}
ajaxRequest.open("GET", "serverTime.php", true);
ajaxRequest.send(null);
How is it possible and how does this code work , when the "change state"
property is checked
BEFORE
the open and send commands are executed ?
I know it works...but how does the flow return back to "check the state
status" after the "open" and "send" are executed.
I would appreciate any help
Many thanks in advance :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为 onreadystatechange 是一个事件,并且在就绪状态更改之前不会调用该代码,这将在请求完成后的某个时刻发生。
Because onreadystatechange is an event, and that code won't get invoked until the ready state changes, which will occur at some point in the future when the request is complete.
发出请求
您已经有了闪亮的新 XMLHttpRequest 对象;现在试一试。首先,您需要一个 Web 页面可以调用的 JavaScript 方法(例如当用户键入文本或从菜单中选择一个选项时)。然后,您将在几乎所有 Ajax 应用程序中遵循相同的基本轮廓:
处理响应
现在您需要实际处理服务器的响应。此时您实际上只需要知道两件事:
Making a request
You have your shiny new XMLHttpRequest object; now take it for a spin. First, you need a JavaScript method that your Web page can call (like when a user types in text or selects an option from a menu). Then, you'll follow the same basic outline in almost all of your Ajax applications:
Handling the response
Now you need to actually deal with the server's response. You really only need to know two things at this point: