実現したいこと
VSCodeのGoLiveで実行しています。
現代の JavaScript チュートリアル
javascript
1let user = { 2 name: 'John', 3 surname: 'Smith' 4}; 5 6let response = await fetch('/article/fetch-basics/post/user', { 7 method: 'POST', 8 headers: { 9 'Content-Type': 'application/json;charset=utf-8' 10 }, 11 body: JSON.stringify(user) 12}); 13 14let result = await response.json(); 15alert(result.message);
実行結果:以下のように表示したい
User saved.
発生している問題・分からないこと
本体のコードには(async () => {がついていないのでつけています。
Google Chrome consoleで表示されるエラーです。:
Uncaught (in promise) SyntaxError: Unexpected end of JSON input
at index3-2.html:50:33
エラーメッセージ
error
1Google Chrome console: 2Uncaught (in promise) SyntaxError: Unexpected end of JSON input 3 at fetch2.js:17:31
該当のソースコード
javascript
1<!DOCTYPE html> 2<script> 3"use strict"; 4 5(async () => { 6let user = { 7 name: 'John', 8 surname: 'Smith' 9}; 10 11let response = await fetch('/article/fetch-basics/post/user', { 12 method: 'POST', 13 headers: { 14 'Content-Type': 'application/json;charset=utf-8' 15 }, 16 body: JSON.stringify(user) 17}); 18 19let result = await response.json(); 20alert(result.message); 21})() 22</script>
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
javascript
1(async () => { 2 3 let user = { 4 name: 'John', 5 surname: 'Smith' 6 }; 7 8 let response = await fetch('http://127.0.0.1:5500/article/fetch-basics/post/user', { 9 10 method: 'POST', 11 headers: { 12 'Content-Type': 'application/json;charset=utf-8' 13 }, 14 body: JSON.stringify(user) 15 }); 16 17 let result = await response.json(); 18 alert(result.message); 19})()
結果は変わりませんでした:
Uncaught (in promise) SyntaxError: Unexpected end of JSON input
at index3-2.html:50:33
補足
localhostで実行できるようにしたいので
よろしくお願いいたします

回答2件
あなたの回答
tips
プレビュー