LoginSignup
5
3

More than 5 years have passed since last update.

HTML と JavaScript が shift_jis の場合の JSON の文字コードについて

Last updated at Posted at 2018-04-17

比較

以下を変えて比較

  • JSON の読み込み方
  • サーバのレスポンスヘッダの Content-Type の文字コード
  • JSON の文字コード

XHR の場合

Content-Type   JSON  結果       
utf-8(指定なし) shift_jis JSONパースエラー
Content-Type   JSON  結果       
shift_jis shift_jis 正常
Content-Type   JSON  結果       
utf-8(指定なし) utf-8 JSONパースエラー
Content-Type   JSON  結果       
shift_jis utf-8 文字化け

Content-Type と JOSN の文字コードが shift_jis の場合に正常に動作。

Fetch API の場合

Content-Type   JSON  結果       
utf-8(指定なし) shift_jis JSONパースエラー
Content-Type   JSON  結果       
shift_jis shift_jis JSONパースエラー
Content-Type   JSON  結果       
utf-8(指定なし) utf-8 正常
Content-Type   JSON  結果       
shift_jis utf-8 正常

JOSN の文字コードが utf-8 の場合に正常に動作。

結論

Content-Type の文字コードがなんであろうと、Fetch API で読み込み JSON の文字コードを utf-8 にしておけば大丈夫そうな感じではある。

5
3
0

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
5
3