微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 如何使用2 XMLHttpRequest一个依赖于另一个?

我在一个项目上工作,我有2个 XMLHttpRequest()对象,比如A和B.

我想要完成的是当A完成获取数据项列表时,B将被触发以根据以前提取的数据项获取一些更多的项目.

目前我的问题是两个对象彼此独立工作.

我的代码如下:

var A = new XMLHttpRequest();

            var B = new XMLHttpRequest();

            A.open("GET",directory,true);
            A.onreadystatechange = function () {

                if (A.readyState === 4) {
                    if (A.status === 200 || A.status == 0) {
                     //does... something
                     }
                }

            }
            A.send(null);
            while(true){

                B.open("GET",another_directory,false);
                B.overrideMimeType("application/document");
                B.send(null);
                if (B.status == "404")
                continue;

                 //does... something else
            }

代码不工作,因为我可以在A完成之前找到evertime B.我基本上不知道要使用哪个事件.

我如何才能实现我的目标?
可以使用什么事件,以便在完成A之后可以同步处理B?

解决方法

好的,让我们开始你的代码.我已经添加了一些意见,所以现在你可以了解问题的根源:
var A = new XMLHttpRequest(); //You create an XMLHttpRequest object
var B = new XMLHttpRequest(); //And an another

A.open("GET",true); 

/* Now you open a GET request to DIRECTORY,with async TRUE. The third parameter can 
make a request sync or async,but sync is not recommended as described below. */

A.onreadystatechange = function () {
    if (A.readyState === 4) {
        if (A.status === 200 || A.status == 0) {

        /* So you registered an event listener. It runs when the readyState changes.
        You can use it to detect if the request is finished or not. If the readyState is
        4,then the request is finished,if the status code is 200,then the response is
        OK. Here you can do everythin you want after the request. */

         }
    }

}

A.send(null); //Now you send the request. When it finishes,the event handler will
// do the processing,but the execution won't stop here,it immediately goes to the 
// next function

while(true){ // Infinite loop
     B.open("GET",false); //Open request B to ANOTHER_DIRECTORY,// but Now,request B will be synchronous

     B.overrideMimeType("application/document"); // Configure mime type

     B.send(null); // Send the request

     if (B.status == "404")
         continue;
         // If it's not found,then go to the next iteration

     // and do something else
}

我希望现在你可以看到问题的根源.当您运行此脚本时,您将启动一个异步请求,然后立即启动下一个.现在你可以从2种方式中选择.

从回调运行下一个请求(推荐)

这是更好的方法.因此,启动您的第一个(异步)请求,并在事件侦听器(您进行处理的地方)中启动下一个请求.我在这里发表了一个评论的例子:http://jsfiddle.net/5pt6j1mo/1/

(你可以没有数组来做 – 这只是一个例子)

如果您使用这种方式,那么在等待响应之前,GUI不会冻结.一切都将负责,所以你可以与页面进行交互,你可以创建取消按钮等.

同步AJAX(不推荐)

我不推荐它,因为“Chrome上同步XMLHttpRequest在主线程已被弃用”,但如果你真的想,那么你可以尝试使用这个解决方案.所以XMLHttpRequest的open函数有3个参数:

>方法:要使用哪个HTTP methid
> URL:要请求的URL
> ASYNC:异步请求?如果为false,那么它将是同步的,这意味着在调用.send()之后,它将暂停执行,直到响应返回.

所以如果你把第三个参数设置为FALSE,那么你可以轻松地做到这一点…但是你不应该!

原文地址:https://www.jb51.cc/js/154341.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐