Ajax XMLHttpRequest JSON

 


const xhr = new XMLHttpRequest();

// リクエスト
xhr.open("GET", 'https://jsonplaceholder.typicode.com/todos/');

//リクエスト送信
xhr.send();

// 自動的に呼ばれる関数
xhr.onreadystatechange = function () {
    // readyState XMLHttpRequest の状態 4: リクエストが終了して準備が完了
    // status httpステータス
    if (xhr.readyState == 4 && xhr.status == 200) {
        // jsonをオブジェクトに変更
        const jsonObj = JSON.parse(xhr.responseText);

        for (let item of jsonObj) {
            console.log("id: " + item.id + " title: " + item.title + " completed: " + item.completed);
        }

    }
}

コメント

このブログの人気の投稿

オンボード・グロープラグヒート製作(On-board glow plug heat circuit)①構想編

オンボード・グロープラグヒート製作(On-board glow plug heat circuit)④ソフトウェア編

回路シミュレータ LTSPICE