无法弄清楚如何从此函数返回值(字符串):
function loadPage(url) {
var xhttp = new XMLHttpRequest();
xhttp.open("GET", url, true);
xhttp.send();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
//var html = xhttp.responseText
var html = document.documentElement.outerHTML;
return html
}
}
}
console.log(loadPage("http://stops.lt/vilnius/#trol/2/a-b"))
当我console.log(html)
在里面使用时,xhttp.onreadystatechange
它会打印出正确的结果,但我不明白如何返回loadPage(url)
. 尝试了这些工作,但都没有return xhttp.onreadystatechange
。return xhttp.onreadystatechange()