let loadButton = document.getElementById('loadButton'); loadButton.addEventListener('click', function() { var xhr = new XMLHttpRequest(); xhr.open("GET", "adat.txt"); xhr.send(); xhr.onload = function() { if(xhr.readyState === xhr.DONE) { if(xhr.status === 200) { console.log(xhr.responseText); } } } });
httpRequest.open("GET","feldolgoz.php?vnev=Nagy&knev=Joska", true); httpRequest.send();
function getEmployees() { let url = 'http://localhost:8000/employees'; let http = new XMLHttpRequest(); http.open('get', url, true); http.onload = function() { console.log(http.responseText) } http.send(); } getEmployees();
function getEmployees() { let url = 'http://localhost:8000/employees'; let http = new XMLHttpRequest(); http.open('get', url, true); http.onload = function() { if(http.readyState === http.DONE) { if(http.status === 200) { console.log(http.responseText) } } } http.send(); } getEmployees();
Ha nem helyben szeretnénk felhasználni a lekérés eredményét, akkor Promise készítése a megoldás.
getEmployees().then( response => { console.log(JSON.parse(response)) }) .catch(function(error) { console.error(error); }) function getEmployees() { return new Promise(function(resolve, reject) { let url = "http://localhost:8000/api/employees"; let http = new XMLHttpRequest(); http.open('get', url, true); http.send(); http.onload = function() { if(http.status == 200) { resolve(http.responseText); }else { reject('Hiba! A lekérés sikertelen!') } } http.onerror = function() { reject('Hiba! A hálózat nem elérhető!') } }) }