Ruby on rails X-Message中的Unicode仅在chrome中正确
我在Safari和Firefox中显示UTF-8字符串时遇到问题,无法通过X-Message头在rails中为ajax请求发送flash消息,如下所示:Ruby on rails X-Message中的Unicode仅在chrome中正确,ruby-on-rails,unicode,chinese-locale,Ruby On Rails,Unicode,Chinese Locale,我在Safari和Firefox中显示UTF-8字符串时遇到问题,无法通过X-Message头在rails中为ajax请求发送flash消息,如下所示: class ApplicationController < ActionController::Base def flash_to_headers return unless request.xhr? message = flash_message message = "#{message.j
class ApplicationController < ActionController::Base
def flash_to_headers
return unless request.xhr?
message = flash_message
message = "#{message.join("','")}" if message.is_a?(Array)
response.headers['X-Message'] = message
response.headers["X-Message-Type"] = flash_type.to_s
flash.discard # don't want the flash to appear when you reload page
end
def flash_message
[:red, :warning, :notice, :success, nil].each do |type|
return "" if type.nil?
return flash[type] unless flash[type].blank?
end
end
def flash_type
[:red, :warning, :notice, :success, nil].each do |type|
return "" if type.nil?
return type unless flash[type].blank?
end
end
class ApplicationController
处理标头的Javascript:
var fade_flash = function() {
$(".flash_alert").fadeIn("fast");
$(".flash_alert").delay(10000).fadeOut("slow");
};
var show_ajax_message = function(msg, type) {
$(".flash_message").html('<div class="flash_alert hidden flash_'+type+'">'+msg+'</div>');
fade_flash();
};
$( document ).ajaxComplete(function(event, request) {
var msg = request.getResponseHeader('X-Message');
var type = request.getResponseHeader('X-Message-Type');
if (msg) {
show_ajax_message(msg, type);
}
});
var fade\u flash=函数(){
$(“.flash_alert”).fadeIn(“fast”);
$(“.flash_alert”)。延迟(10000)。淡出(“慢速”);
};
var show\u ajax\u message=函数(消息,类型){
$(“.flash_message”).html(“”+msg+“”);
淡入淡出闪光();
};
$(文档).ajaxComplete(函数(事件、请求){
var msg=request.getResponseHeader('X-Message');
var type=request.getResponseHeader('X-Message-type');
如果(味精){
显示消息(消息,类型);
}
});
在我添加带有中文支持的l18n语言环境之前,它一直工作得很好。在Chrome中,它仍然有效,但在Safari和Firefox中,flash消息不再正确显示
例如:
这样的信息:投注金额” 比你的余额多" 将在Chrome fine中显示,但在Safari和Firefox中显示为:“浏览器”
你可以在网站上看到这个bug的实时演示
只需单击中文区域设置下的滚动,您就可以看到flash消息是不正确的,除非使用chrome。甚至引号在英文区域设置中也没有正确显示
提前感谢您的帮助!RFC 2616指定HTTP头来承载ISO-8859-1数据。Safari和Firefox做的是正确的。根据HTTP标准,不可能在HTTP头值中添加中文字符 这里的浏览器行为不一致(Opera也使用UTF-8,IE使用任何语言环境ANSI代码页),因此在HTTP头中使用非ASCII字符是不安全的
如果必须使用头,通常的解决方法是在每一端应用特殊编码,通常是URL编码或UTF-8字节上的base64编码。但通常最好不要将任意数据包装在HTTP头中,而是将其放在正文中。我最终解决了这个问题,没有使用X-Message头。但是,我仍然不确定为什么使用original解决方案在Firefox和Safari中不起作用。