jQuery.ajax() ã®ãªã¯ã¨ã¹ããã©ã¡ã¼ã¿ããæå®ããæåã³ã¼ãã§æ¸¡ã
ãjQuery.ajax() ã®ã¬ã¹ãã³ã¹ãæå®ããæåã³ã¼ãã§åå¾ããç³»ã®è©±ã¯ãã®è¾ºã«ãããã転ãã£ã¦ãã¾ãããéã«ãªã¯ã¨ã¹ããæå®ããæåã³ã¼ãã§è¡ãç³»ã®è©±ãã»ã¨ãã©è¦ã¤ãããªãã£ãã®ã§ãã©ã¤ãã©ãªãä½æãã¾ããã
ãèãæ¹ã¨ãã¦ã¯ãé常㯠UTF-8 ã§è¡ããããªã¯ã¨ã¹ããã©ã¡ã¼ã¿ã® URI ã¨ã³ã³ã¼ãå¦çããShiftJIS ã EUC-JP ã§è¡ã£ã¦ããã°ããã ãã§ããâ¦â¦ã¨ã¯ãããæååã ShiftJIS ã EUC-JP ã«å¤æããé¨åã¯å¤§å¤ãªã®ã§ãã©ã¤ãã©ãªä»»ãã«ãã¦ãã¾ã£ã¦ãã¾ãã
ãUTF-8 ããä»ã®æåã³ã¼ãã¸ã®å¤æå¦çã«ã¯ãhttp://polygon-planet-log.blogspot.jp/2012/04/javascript.html ã® encoder.js ã使ç¨ãã¦ãã¾ããä¸è¨ã³ã¼ãã®å©ç¨ã«ã¯ãencoder.jsï¼https://raw.github.com/polygonplanet/Unzipper.js/master/src/encoding.j...ï¼ãå¿
è¦ã¨ãªãã¾ãã
ãä»»æã®æåã³ã¼ãã«å¯¾ãã URI ã¨ã³ã³ã¼ãé¢æ°ãèªåã§ç»é²ãããã¨ãã§ããããã«ãªã£ã¦ãã¾ããencoder.js ã使ãããå¥ã®æåã³ã¼ãå¤æã©ã¤ãã©ãªã使ãå ´åããå¾ãã jQuery.ajaxCharcode.encoders ã« URI ã¨ã³ã³ã¼ãé¢æ°ãç»é²ãã¦ããã ãã§æ§ãã¾ããã
(function($){ $.ajaxCharcode = { charcode: 'auto', // 'auto' ã§ã¯ãHTMLããã¥ã¡ã³ãã®æåã³ã¼ãã¸ã®å¤æãè¡ã encoders: { // 'utf-8', 'utf8' ã§ã¯ãé常éã encodeURIComponent() ãç¨ããã // 'none' ã§ã¯URLã¨ã³ã³ã¼ããè¡ããªã // jQuery.ajaxCharcode.encodersã«ç¬èªã®URLã¨ã³ã³ã¼ãå¦çã追å ãããã¨ã§ãä»»æã®æåã³ã¼ãã«ã対å¿å¯è½ 'none': noConvert, } }; if (typeof Encoding !== 'undefined' && Encoding.urlEncode && Encoding.convert) { (function() { $.extend($.ajaxCharcode.encoders, { // ShiftJIS 'shiftjis': encodeSJIS, 'shift_jis': encodeSJIS, 'shift-jis': encodeSJIS, 'sjis': encodeSJIS, 'x-sjis': encodeSJIS, // EUC-JP 'euc-jp': encodeEUC_JP, 'eucjp': encodeEUC_JP, // JIS 'jis': encodeJIS }); // æååâæåã³ã¼ãã®é å function toArray(text) { var length = text.length; var array = new Array(length); for (var i = 0; i < length; ++i) array[i] = text.charCodeAt(i); return array; } // ShiftJIS function encodeSJIS(text) { return Encoding.urlEncode(Encoding.convert(toArray(text), 'SJIS', 'UNICODE')); } // JIS function encodeJIS(text) { return Encoding.urlEncode(Encoding.convert(toArray(text), 'JIS', 'UNICODE')); } // EUC function encodeEUC_JP(text) { return Encoding.urlEncode(Encoding.convert(toArray(text), 'EUCJP', 'UNICODE')); } })(); } // ããã©ã«ãã® encodeURIComponent() var defaultEncodeURIComponent = window.encodeURIComponent; // ããã©ã«ãã® jQuery.param() var old_param = $.param; // ä¸æ¸ãçã® jQuery.param() $.param = function() { var charcode = String($.ajaxCharcode.charcode).toLowerCase(); if (charcode === 'utf-8' || charcode === 'utf8') { // UTF-8æå®ã§ããã°ãããã©ã«ãã® encodeURIComponent() ãç¨ãã¦ãã©ã¡ã¼ã¿æååãçæ return old_param.apply(this, arguments); } if (charcode === 'auto') { // autoæå®ã§ããã°ãHTMLããã¥ã¡ã³ãã®æåã³ã¼ããç¨ãã charcode = String(document.charset || document.characterSet).toLowerCase(); } // æå®ããæåã³ã¼ãã«å¯¾å¿ããå¤æé¢æ°ããããã©ãããç¢ºèª var converter = $.ajaxCharcode.encoders[charcode]; if (!converter) { return old_param.apply(this, arguments); } // ä¸æçã« encodeURIComponent() ã®åä½ãä¸æ¸ã window.encodeURIComponent = converter; try { // ä¸æ¸ãããã encodeURIComponent() ãç¨ãã¦ãã©ã¡ã¼ã¿æååãçæ return old_param.apply(this, arguments); } finally { // encodeURIComponent() ã®åä½ãæ¬æ¥ã®ãã®ã«æ»ã window.encodeURIComponent = defaultEncodeURIComponent; } }; // å¤æãªã function noConvert(text) { return text; } })(jQuery);
ããã£ã¦ãäºã¯ã極ãã¦åç´ã
ãjQuery ã®ãªã¯ã¨ã¹ããã©ã¡ã¼ã¿ã®çæã¡ã½ãããä¸æ¸ãããã°ãã¼ãã«ãª encodeURIComponent() ã®ä»£ããã«ãæå®ããæåã³ã¼ãï¼ããã©ã«ãã§ã¯ HTML ã®æåã³ã¼ãã¨åãï¼ç¨ã® URI ã¨ã³ã³ã¼ãå¦çã使ãããã«ãã¦ããã ãã§ãã
ãããã©ã«ãã§ã¯ jQuery.ajaxCharcode.charcode ã« 'auto' ãè¨å®ãã¦ãããããä¸è¨ã¹ã¯ãªããã追å ããã ãã§å
¨ã¦ã® jQuery.ajax() ã®åä½ã HTML ã®æåã³ã¼ãã§è¡ãããããã«ãªãã¾ããé常㮠Ajax é信㯠UTF-8 ã®ã¾ã¾ã«ãã¦ããããå ´åã¯ãã¹ã¯ãªããèªã¿è¾¼ã¿å¾ã« jQuery.ajaxCharcode.charcode = 'utf-8'; ã追å ãã¦ä¸ããï¼UTF-8 以å¤ã«ãããå ´æã®ç´å¾ã ããæåã³ã¼ããæå®ãããã¨ã«ãªãã¾ãï¼ã
ãä¸è¨ã¹ã¯ãªããã¯ãjQuery Form ã® ajaxSubmit() ã«ãæå¹ã§ãï¼ãã ããmultipart/form-data 㧠POST ããå ´åãé¤ãï¼ã
$('#form').on('submit', function() { jQuery.ajaxCharcode.charcode = 'Shift_JIS' ; $(this).ajaxSubmit({ success: function(html) { ... } }); jQuery.ajaxCharcode.charcode = 'auto' ; return false; });