简体   繁体   中英

Can't access data or copy data from XMLHttpRequest when Synchronous javascript AJAX

How to access the information received from the XMLHttpRequest when XMLHttpRequest is synchronous?

 // JavaScript Pure and AJAX let data = []; let xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function () { if (this.readyState == 4 && this.status == 200) { let a = JSON.parse(this.responseText); let xhttp = new XMLHttpRequest(); //declear new obj in previus obj xhttp.onreadystatechange = function () { if (this.readyState == 4 && this.status == 200) { let b = JSON.parse(this.responseText); for (let i = 0; i < a.personInfo.length; i++) { for (let j = 0; j < a.personInfo.length; j++){ if (a.personInfo[i].uid === b.additionalPersonInfo[j].uid) { data[i] = Object.assign(a.personInfo[i], b.additionalPersonInfo[j]) } } } } }; xhttp.open("GET", "https://api.npoint.io/dc6cb50568fac72a4105", true); xhttp.send(); } //end bulid in obj }; xhttp.open("GET", "https://api.npoint.io/177cea9c157de479d51b", true); xhttp.send(); for (let o = 0; o < data.length; o++) { console.log(data[o]); }

I want access data[]

  1. You should not use synchronous AJAX. This is deprecated and will give a bad user experience. However, your ajax calls are not synchronous in the code you have provided.

  2. Your inner loop should run through b.additionalPersonInfo . But you are running through a.personalInfo in the inner loop too.

  3. You should push to your data array instead of trying to add data to yet non-existing elements.

  4. Your data can only be accessed from within the callbacks - that is only after the data is really available.


    let data = [];

    let xhttp = new XMLHttpRequest();

    xhttp.onreadystatechange = function () {

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

            let a = JSON.parse(this.responseText);

            let xhttp = new XMLHttpRequest(); //declear new obj in previus obj

            xhttp.onreadystatechange = function () {

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

                    let b = JSON.parse(this.responseText);

                    for (let i = 0; i < a.personInfo.length; i++) {
                        for (let j = 0; j <  b.additionalPersonInfo.length; j++){
                            if (a.personInfo[i].uid === b.additionalPersonInfo[j].uid) {
                                data.push(Object.assign(a.personInfo[i], b.additionalPersonInfo[j]))
                            }
                        }
                    }

                    //Here is the data -----> 
                    for (let o = 0; o < data.length; o++) {
                     console.log(data[o]);
                    }

                }
            };

            xhttp.open("GET", "https://api.npoint.io/dc6cb50568fac72a4105", true);
            xhttp.send();
        } //end bulid in obj
    };

    xhttp.open("GET", "https://api.npoint.io/177cea9c157de479d51b", true);
    xhttp.send();


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM