Answer the question
In order to leave comments, you need to log in
How to return value to parent function?
function getFile(url) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
return xhr.responseText;
}
}
xhr.open('GET', url);
xhr.send();
}
function somefunc() {
return getFile('http://domain/n.php');
}
console.log(somefunc()) //undefined
Answer the question
In order to leave comments, you need to log in
function getFile(url, cb) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
if (typeof cb === 'function') {
cb(xhr.responseText);
}
}
}
xhr.open('GET', url);
xhr.send();
}
return getFile('http://domain/n.php', function (data) { console.log(data); });
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question