値は置き換えられず、関数は0を返します。 (反応ネイティブ0.30、IOS 10.0シミュレータ)関数からXMLHttpRequestレスポンスを返す方法は?
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;
}
リクエストは、リクエストが送信され、あなたの関数はNULL値を返し、その後、いくつかの時間後にリクエストが完了したことを意味し、非同期です。コールバック関数を 'getCategoryList()'(簡単)または約束(少し難しいが、それほど多くない)に渡すことに注目してください。 – Archer
Read:http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Cherniv
[promise](https://developer.mozilla)を検索してください。 .org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) –