0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

async await を使ったfetch

Last updated at Posted at 2025-06-11

try catch 仕組み

  async ~ + try (fetch始めるよ)
  データ送信するよメソッド  お決まり文定義
  if HTTP通信できなかったら
  HTTP通信失敗したよと表示
  HTTP通信できたらHTTP通信できたよと表示
 data送信できたら
    させたい処理を書く
 データ送信できなかったら <catch>
 データ送れなかったよと表示


 ルール : 処理ができたらawaitを必ずかき
         失敗したらawaitは書かない


//テーブルにdata追加 code化すると
fetch始めるよ
async function createData(data) {
    try {

        
  データ送信するよメソッド  お決まり文定義
      const response = await fetch('https://jsonplaceholder.typicode.com/users', { // ダミーAPI
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(data)
      });
             
             
    //if HTTP通信できなかったら
      if (!response.ok) {

    //HTTP通信失敗したよ、と表示
        throw new Error(`HTTP error! status: ${response.status}`);
              
      }
      HTTP通信できたらHTTP通信できたよと表示
      const result = await response.json();
      console.log('APIから新規データが追加されました:', result);
      
                
  //data送信できたら、データ保存・データ更新
  データ削除 /  入力formを空に
      studentData.push({ ...data, id: nextId++ }); // IDを更新
      saveDataToLocalStorage();
      renderTable();
      clearFormInputs();
            
    } catch (error) {
      console.error('データ追加に失敗しました:', error);
      alert('データ追加に失敗しました。');
    }
  }

ばつボタン 仕組み

 ばつボタンはspan

parseInt 数字を整数にしてdata返却
 066  66
0
0
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?