Но я на нативном JS пишу. додумался только до этого, но правильно ли?
window.onload = function () {
document.querySelector("#callback").onclick = function () {
const url = "https://randomuser.me/api?results=";
ajaxGet(3, url + 3, getName);
}
}
function getName(s, request) {
let data = JSON.parse(request.responseText);
for (let i in data.results) {
document.getElementById("name").innerHTML += s + " " + data.results[i].name.first + " " + data.results[i].name.last + "<br>";
}
const url = "https://randomuser.me/api?results=";
let n = randomInteger(5, 10);
request.onreadystatechange = function () {
if (request.readyState == 4 && request.status == 200) {
let dataq = JSON.parse(request.responseText);
for (let i in dataq.results) {
document.getElementById("name").innerHTML += "rand" + " " + dataq.results[i].name.first + " " + dataq.results[i].name.last + "<br>";
}
}
}
request.open('GET', url + n);
request.send();
}
function ajaxGet(s, url, callback) {
let request = new XMLHttpRequest();
request.onreadystatechange = function () {
if (request.readyState == 4 && request.status == 200) {
callback(s, this);
}
}
request.open('GET', url);
request.send();
}
function randomInteger(min, max) {
var rand = min - 0.5 + Math.random() * (max - min + 1)
rand = Math.round(rand);
return rand;
}