简体   繁体   English

JS XMLHttpRequest作为功能不起作用

[英]JS XMLHttpRequest as function doesn't work

I am a beginner and it may seem to you as a very dumb question, but I can't still figure out why this code doesn't work. 我是一个初学者,在您看来这可能是一个非常愚蠢的问题,但是我仍然无法弄清楚为什么这段代码无法正常工作。 URL is correct, server should respond, but still getting nothing. URL是正确的,服务器应该响应,但仍然无响应。

function request(data, item) {
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            data = JSON.parse(xhr.responseText);
            return data;
        } else {
            alert("Something went wrong!");
        }
    };
    xhr.open("GET", "http://somewebsite.com/" + item, false);
    xhr.send();
}

items = ["item1", "item2", "item3"...];

for (var i=0; i>items.lenght; i++) {
        request(history, items[i]);
        // code for processing history object
}

I want to get JSON response saved in history and then process it 我想获取保存在历史记录中的JSON响应,然后对其进行处理

我弄错了length ,现在可以了,谢谢梅尔登!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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