function ajax(method, url,foo) { let xhr = new XMLHttpRequest(); xhr.open(method, url); xhr.send(); xhr.onreadystatechange = function () { if (xhr.readyState === 4) { if (xhr.status === 200) { // console.log(xhr.responseText); let data = JSON.parse(xhr.responseText); foo(data); // return data; } } } }