参数“true”在 xmlHttpRequest .open() 方法中

发布于 2024-11-17 10:33:48 字数 884 浏览 0 评论 0原文

从我在 MDN 中读到的参考资料来看,它说

如果为 TRUE(默认值),则在服务器响应尚未到达时 JavaScript 函数将继续执行。

这是 AJAX 中的 A。

我一直在使用 AJAX,但当我读到它时我有点困惑。我认为问题可能是我没有清楚地理解AJAX概念。我当然知道 AJAX 不会刷新页面,这意味着与服务器的连接和响应完全在后台完成。

但我可以想象根据该引用发生的情况是,如果我在 JavaScript 中有这样的代码:

//true, therefore process the function while server retrieves url
var xmlResponse;
var url = "http://example.com/file.xml";
xml_req.open("GET", url, true); 

xml_req.onreadystatechange = function() {
     if(xml_req.readyState == 4 && xml_req.status == 200) {
        if(xml_req.responseText != null)
             xmlResponse = xml_req.responseXML; //server response may not yet arrive
        else {
             alert("failed");
             return false;
        }
     };
xml_req.send(null);

这是否意味着 xmlResponse 可能是未定义的,因为服务器仍在检索数据?有人能解释一下 AJAX 技术的执行流程到底是什么吗?提前致谢。

From the reference I read in MDN, it says

If TRUE (the default), the execution of the JavaScript function will continue while the response of the server has not yet arrived.

This is the A in AJAX.

I have been using AJAX but then I was a little confused when I read that. I think the problem may be that I am not understanding AJAX concept clearly. I know of course AJAX does not refresh the page which means the connection to the server and the response are completely done in the background.

But what I can imagine happening according to that reference is that if I have a code like this in my JavaScript:

//true, therefore process the function while server retrieves url
var xmlResponse;
var url = "http://example.com/file.xml";
xml_req.open("GET", url, true); 

xml_req.onreadystatechange = function() {
     if(xml_req.readyState == 4 && xml_req.status == 200) {
        if(xml_req.responseText != null)
             xmlResponse = xml_req.responseXML; //server response may not yet arrive
        else {
             alert("failed");
             return false;
        }
     };
xml_req.send(null);

Doesn't that mean xmlResponse could be undefined in the sense that the server is still retrieving the data? Could somebody explain what really is the flow of the execution in AJAX technology? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愁以何悠 2024-11-24 10:33:48

我在这里写了一篇更详细的文章,但这就是基本思想。

将其设置为 true 意味着您正在发出异步请求。这意味着在 http 请求完成之前代码不会暂停。同步调用会锁定浏览器,因此其他任何操作都不会运行。这可能会导致问题,因此人们更喜欢异步。

XHR 对象向我们更新它正在做什么。它为我们提供了 onreadystatechange 事件的更新。我们向它注册一个函数,以便我们可以跟踪它的状态。 onreadystatechange 被调用 4 次。每个都有不同的状态

0 = uninitialized
1 = loading
2 = loaded
3 = interactive
4 = complete

当readystate为4时数据可供我们使用。

现在在您发布的代码中,它正在检查完整状态并确保状态为200 [ok]

if(xml_req.readyState == 4 && xml_req.status == 200){

xmlResponse的值将是未定义的如果您在返回之前尝试在代码中的其他地方使用它。示例

ml_req.send(null);
alert(xmlResponse );

关于 XMLHttpRequest 的第一篇文章可能适合您阅读。 Apple 关于 xmlhttpreq 的文章

I wrote a more detailed article here, but this is the basic idea.

Setting it to true means you are making an asynchronous request. That means the code does not pause until the http request is complete. A synchronous call locks up the browser so nothing else runs. That can cause problems, so people prefer asynchronous.

The XHR object updates us on what it is doing. It gives us the updates with the onreadystatechange event. We register a function with it so we can keep track of its status. The onreadystatechange gets called 4 times. Each with a different state

0 = uninitialized
1 = loading
2 = loaded
3 = interactive
4 = complete

The data is available to us when the readystate is 4.

Now in the code you posted, it is checking for the complete state and it makes sure that the status is 200 [ok]

if(xml_req.readyState == 4 && xml_req.status == 200){

The value for xmlResponse will be undefined if you try to use it somewhere else in the code before it is returned. An example

ml_req.send(null);
alert(xmlResponse );

One of the very first articles on the XMLHttpRequest article might be a good read for you. Apple Article on xmlhttpreq

苍风燃霜 2024-11-24 10:33:48

需要了解的重要一点是,您的 onreadystatechange 处理程序不会立即执行。并且它被执行了不止一次。如果将各个部分分解为单独的函数,那么概念化可能会更容易:

function makeRequest(url)
{
    var xhr = new XMLHttpRequest();
    xhr.open("GET", url, true);
    xhr.onreadystatechange = receiveResponse;
    xhr.send();
}
function receiveResponse(e)
{
    if (this.readyState == 4)
    {
        // xhr.readyState == 4, so we've received the complete server response
        if (this.status == 200)
        {
            // xhr.status == 200, so the response is good
            var response = this.responseXML;
            ...
        }
    }
}

首先,调用 makeRequest 然后退出。然后,一旦我们收到服务器返回的任何信息,就会调用 receiveResponse 。每次,我们都会检查是否完全收到响应,然后才继续处理该响应。

The important thing to understand is that your onreadystatechange handler is not executed immediately. And it is executed more than once. It may be easier to conceptualize, if you break the pieces out into individual functions:

function makeRequest(url)
{
    var xhr = new XMLHttpRequest();
    xhr.open("GET", url, true);
    xhr.onreadystatechange = receiveResponse;
    xhr.send();
}
function receiveResponse(e)
{
    if (this.readyState == 4)
    {
        // xhr.readyState == 4, so we've received the complete server response
        if (this.status == 200)
        {
            // xhr.status == 200, so the response is good
            var response = this.responseXML;
            ...
        }
    }
}

First, makeRequest is called and then exits. Then, as soon as we hear anything back from the server, receiveResponse is called. Each time, we check to see if the response is fully received, and only then do we continue to process that response.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文