【发布时间】:2016-12-14 06:03:39
【问题描述】:
值不是替换,函数返回 0。如何解决? (react-native 0.30, IOS 10.0 模拟器)
export function getCategoryList() {
var xhr = new XMLHttpRequest();
jsonResponse = null;
xhr.onreadystatechange = (e) => {
if (xhr.readyState !== 4) {
return;
}
if (xhr.status === 200) {
console.log('SUCCESS', xhr.responseText);
jsonResponse = JSON.parse(xhr.responseText);
} else {
console.warn('request_error');
}
};
xhr.open('GET', 'https://httpbin.org/user-agent');
xhr.send();
return jsonResponse;
}
【问题讨论】:
-
请求是异步的,这意味着请求被发送,你的函数返回一个空值,然后一段时间后请求完成。看看将回调函数传递给
getCategoryList()(简单)或promise(有点困难,但不多)。 -
你应该寻找promise
标签: javascript networking xmlhttprequest react-native