jquery - How to encode/decode UTF-8 in HTTP headers in Rails? -
i using "insert rails flash messages http headers show ajax response errors" pattern, this:
controller:
def flash_as_header return unless request.xhr? [:error, :warning, :notice].each |type| if flash[type] response.headers["x-ajax-#{type.to_s.humanize}"] = flash[type] end end end
jquery:
$(document).ajaxcomplete(function(response, status, xhr) { var types = ["error", "notice", "warning"]; (i = 0, l = types.length; < l; ++i) { msg = status.getresponseheader("x-ajax-" + types[i]); if(msg) { break; } } if(msg) { $('.flash-error').text(msg).removeclass('is-hidden'); } });
this works, running character encoding issues. flash messages contain utf-8 special characters, , destination html document utf-8.
here sample string, should appear:
användare
this how in http response (correct: %c3%a4
ä):
x-ajax-error:anv%c3%a4ndare
and how outputs in html document:
användare
that consistent table (http://www.i18nqa.com/debug/utf8-debug.html) says "the problem being caused utf-8 bytes being interpreted windows-1252 (or iso 8859-1) bytes."
i unsure how & fix this- in jquery function, in rails controller, or in html template?
a combination of escaping & decoding in javascript has worked:
i changed this:
$('.flash-error').text(msg).removeclass('is-hidden');
to this:
$('.flash-error').text(decodeuricomponent(escape(msg))).removeclass('is-hidden');
the output correct.
Comments
Post a Comment