🐕 [Q1] POST通信がまだ謎・・・
作成日: 2021/12/25
1

POST通信は、既存のJSONにparamsで記述したJSONを追加するということなのだろうか?

コード

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    

    <script>
        const url = 'https://jsonplaceholder.typicode.com/posts'
        const params = {
            name: '山田太郎',
            title: 'taro@example.com',
            password: 'secret'
        };
        fetch(url, {
            method: 'POST',
            headers: {'Content-Type': 'application/json'},
            body: JSON.stringify(params)
        })
        .then(res => res.json())
        .then(data => console.log(data))
        .catch(err => console.log(err))
    </script>
</body>
</html>

レスポンス

{name: '山田太郎', title: 'taro@example.com', password: 'secret', id: 101}

事業会社にてコーダーをしています。制作は6年目。 インプットしたことをアウトプットしています。