私は反応で非同期XMLHttpRequestを実装しようとしています。 は、ここに私の試みです:私はそれにリスナーを追加することを考えてきた非同期xmlhttp要求の反応
var xhr = new XMLHttpRequest();
var json_obj, status = false;
xhr.open("GET", "https://jsonplaceholder.typicode.com/photos/", true);
xhr.onload = function (e) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
json_obj = xhr.responseText;
status = true;
} else {
console.error(xhr.statusText);
}
}
};
xhr.onerror = function (e) {
console.error(xhr.statusText);
};
xhr.send(null);
class Welcome extends React.Component {
render() {
return (
<div>
<img src= {status ? json_obj[0].url : 'loading...'}></img>
</div>
);
}
}
ReactDOM.render(
<Welcome/>,
document.getElementById('root')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"></div>
しかし、私はどのように行うのか分かりません。
全体的に私は非同期XMLHttpRequestを読み込んで値を返すと、アップデートに問題があります。
あなたは 'axios'または' fetch'を使うべきです。 – Purgatory
私は以前の回答から得た返信に本当に満足していますが、入力に感謝します。 – Mac