Václav Václav - 1 year ago 109
Javascript Question

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.

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);

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

Answer Source

I mispelled length, now it works, thanks Meldon!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download