Почему моя страница не загружает данные ajax?Кто-нибудь может увидеть в чем проблема?Я правильно вызываю js-скрипт в мой html, но когда я загружаю html-страницу, он переходит к предупреждению и говорит: «Возникла проблема с запросом».Это старый школьный код, который я только что обнаружил давным-давно, и до сих пор не могу решить проблему с ним.Кто-нибудь может определить, в чем проблема?Спасибо
// set a global httpRequest object
var httpRequest;
// TODO: when the page (window) has loaded, make a
// request for page 1
window.onload=function(){
makeRequest(1);
}
function makeRequest(pageNum) {
// TODO: create a variable "url" to store the request to
// the current pageNum, ie:
//
// "http://reqres.in/api/users?page=1" // for page 1
// "http://reqres.in/api/users?page=2" // for page 2
// etc...
var url="http://reqres.in/api/users?page==" + pageNum;
// make an HTTP request object
httpRequest = new XMLHttpRequest();
// show an alert if we were unable to make an XMLHttpRequest object
if (!httpRequest) {
alert('Cannot create an XMLHTTP instance');
return false;
}
// execute the "showContents" function when
// the httprequest.onreadystatechange event is fired
httpRequest.onreadystatechange = showContents;
// open a asynchronous HTTP (GET) request with the specified url
httpRequest.open('GET', url, true);
// send the request
httpRequest.send();
}
// the function that handles the server response
function showContents() {
// check for response state
// 0 The request is not initialized
// 1 The request has been set up
// 2 The request has been sent
// 3 The request is in process
// 4 The request is complete
if (httpRequest.readyState === 4) {
// check the response code
if (httpRequest.status === 200) { // The request has succeeded
// Javascript function JSON.parse to parse JSON data
var jsData = JSON.parse(httpRequest.responseText);
// TODO: use the jsData object to populate the correct
// table cells, ie <tr><td>...</td></tr>
// in the <tbody> element with id="data"
//var tbody=document.querySelector(#data);
for(var i=0;i<jsData.data.length;i++)
{
console.log("ID: " + jsData.data[i].id);
console.log("First Name: " +js.Data.data[i].first_name);
console.log("Last Name: " +js.Data.data[i].last_name);
console.log("Avatar: " +js.Data.data[i].avatar);
}
// TODO: remove the class "active" from all elements with the class "pgbtn"
for(var i=0;i<jsData.data.length;i++)
{
document.getElementById(data[i]).classList.remove(pgbtn);
}
// TODO: add the class "active" to the button corresponding to the active page, ie:
//
// if jsData.page is 1, add the class "active" to button element with id pgbtn1
// if jsData.page is 2, add the class "active" to button element with id pgbtn2
// etc...
} else {
alert('There was a problem with the request.');
}
}
}