【发布时间】:2020-09-13 16:23:05
【问题描述】:
我尝试使用 http 请求和回调函数来获取数据,但是当我尝试打印时我得到未定义 变量。
function httpGetAsync(theUrl, callback)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
callback(xmlHttp.responseText);
}
xmlHttp.open("GET", theUrl, true);
xmlHttp.send(null);
}
var response;
httpGetAsync("https://localhost:44319/api/Food/Get",function(result){
response = result;
})
console.log(response)
【问题讨论】:
标签: javascript http callback return undefined