将数据从XMLHttpRequest转换为数组

时间:2018-10-02 16:37:12

标签: javascript

如何将数据从XHR转换为数组?

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        localStorage.setItem("index", JSON.stringify(this.responseText));
        indexArray = localStorage.getItem("index");
        console.log(typeof indexArray); // returns string, this should be array.
        console.log(indexArray[2]); // must return {10000003: 'How and Why.'}
        console.log(indexArray[2][10000003]); // must return How and Why.
    }
};
xhr.open("GET", "/list?i=" + input.value, true);
xhr.send();

来自/list?i=hello的字符串类型的数据

[{10000001: 'How are you?'}, {10000002: 'Hows your life'}, {10000003: 'How and Why.'}]

0 个答案:

没有答案