LoginSignup
18
19

More than 5 years have passed since last update.

jQueryでShiftJISエンコードされたCSVを処理する

Last updated at Posted at 2014-01-14

想定状況

jquery(1.10)を用いてajaxでCSV(Shift-JIS)を取得する

問題点と解決方法

問題:Shift-JISがうまくエンコードできずに文字化けする

解決方法

1)ファイルのエンコードを変更する

ShiftJISエンコードできるようにまずhtmlとjsをShiftJISにエンコードします。
エンコードにはDreamweaverを使いました。

  • JSファイル(Shift-JIS)
  • HTMLファイル(Shift-JIS) ※meta charset=utf-8になっていないか注意

2)サーバサイドで送出文字コードを指定する

.htaccess

AddDefaultCharset Shift-JIS

補足

overrideMimeTypeが実装されているブラウザ(IE10+,Chrome,Firefox)であれば
以下のようにajax側でmimetypeをオーバーライドすれば対応できます。

$.ajax({
    beforeSend : function(xhr) {
        xhr.overrideMimeType("text/plain; charset=shift_jis");
    },
    url : 'dummy.csv',
    dataType : 'text'
}).done(function(text) {
    //処理
});

参考 IEでもできた! jQuery.ajaxでShift JIS(sjis)の外部HTMLを読み込む時の文字化け回避方法

18
19
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
18
19