Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用ES6进行异步请求处理_Javascript_Ajax_Asynchronous_Ecmascript 6 - Fatal编程技术网

Javascript 使用ES6进行异步请求处理

Javascript 使用ES6进行异步请求处理,javascript,ajax,asynchronous,ecmascript-6,Javascript,Ajax,Asynchronous,Ecmascript 6,我正在尝试异步发送多个请求,但只有一个请求完成了—其余所有请求在整个时间内都具有xmlhttp.status==0,并且xmlhttp.readyState==1,每个请求四次 怎么了 我有两个文件,Api.js和Ajax.js。Api正在使用Ajax发送请求: Api.js: import Ajax from './Ajax'; class Api { returnData (success, failure) { var params = {

我正在尝试异步发送多个请求,但只有一个请求完成了—其余所有请求在整个时间内都具有
xmlhttp.status==0
,并且
xmlhttp.readyState==1
,每个请求四次

怎么了

我有两个文件,
Api.js
Ajax.js
。Api正在使用Ajax发送请求:

Api.js

import Ajax from './Ajax';

class Api {

    returnData (success, failure) {
        var params = {
            methodId: this.ids.returnData,
            requestBody: {}
        };
        this.sendRequest(params, success, failure);
    };

    sendRequest (data, success, failure) {
        Ajax.execute(function (response) {
            success(response); // simplified
        });
    };

}

export default new Api();
class Ajax {

    createXmlHttp () {
        if (window.XMLHttpRequest) {
            this.xmlhttp = new XMLHttpRequest();
        } else {
            this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    };

    onreadystatechange (action, data) {
        this.xmlhttp.onreadystatechange = function () {
            if (this.xmlhttp.readyState === 4) {
                if (this.xmlhttp.status === 200) {
                    action(this.xmlhttp.responseText);
                }
            }
        }.bind(this);
    };

    execute (action, url, data) {
        this.createXmlHttp();
        this.onreadystatechange(action, data);
        this.xmlhttp.open("POST", url, true);
        this.xmlhttp.setRequestHeader("Content-Type", "text/plain");
        this.xmlhttp.send(data);
    };

};

export default new Ajax();
Ajax.js

import Ajax from './Ajax';

class Api {

    returnData (success, failure) {
        var params = {
            methodId: this.ids.returnData,
            requestBody: {}
        };
        this.sendRequest(params, success, failure);
    };

    sendRequest (data, success, failure) {
        Ajax.execute(function (response) {
            success(response); // simplified
        });
    };

}

export default new Api();
class Ajax {

    createXmlHttp () {
        if (window.XMLHttpRequest) {
            this.xmlhttp = new XMLHttpRequest();
        } else {
            this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    };

    onreadystatechange (action, data) {
        this.xmlhttp.onreadystatechange = function () {
            if (this.xmlhttp.readyState === 4) {
                if (this.xmlhttp.status === 200) {
                    action(this.xmlhttp.responseText);
                }
            }
        }.bind(this);
    };

    execute (action, url, data) {
        this.createXmlHttp();
        this.onreadystatechange(action, data);
        this.xmlhttp.open("POST", url, true);
        this.xmlhttp.setRequestHeader("Content-Type", "text/plain");
        this.xmlhttp.send(data);
    };

};

export default new Ajax();

您只能创建Ajax对象的一个实例

每次调用
Ajax.execute
,都会覆盖
this.xmlhttp=new-XMLHttpRequest()

每个请求都需要一个新的
Ajax
实例

不要在那里创建实例:

export default Ajax;
…需要新对象时创建它

sendRequest (data, success, failure) {
    new Ajax.execute(function (response) {

您只能创建Ajax对象的一个实例

每次调用
Ajax.execute
,都会覆盖
this.xmlhttp=new-XMLHttpRequest()

每个请求都需要一个新的
Ajax
实例

不要在那里创建实例:

export default Ajax;
…需要新对象时创建它

sendRequest (data, success, failure) {
    new Ajax.execute(function (response) {

0
不仅适用于不完整的请求,还可能是错误代码。。。而且您编写的代码没有错误处理。
0
不仅适用于不完整的请求,还可能是错误代码。。。您编写的代码没有错误处理。