JSON parse generates url-encoded strings
I never noticed this behavior before, but when parsing a JSON-encoded string saved in a cookie, th开发者_如何学编程e strings seem url-encoded (mainly the symptom is the spaces are replaced with the plus "+" sign).
Basically it's an array of flash messages (as in Rails, I'm not sure if the problem is on the encoding side) encoded using:
def write_flash_to_cookie
cookie_flash = {}
flash.each do |key, value|
if cookie_flash[key.to_s].nil? or cookie_flash[key.to_s].blank?
cookie_flash[key.to_s]= [value]
else
cookie_flash[key.to_s]<< value
end
end
cookies['flash']= cookie_flash.to_json
flash.clear
end
And displayed as such:
if($.cookie('flash')) {
var flash= $.parseJSON($.cookie('flash'));
for(type in flash) {
for(message in flash[type]) {
$('#flash').append("<div class=\"" + type + "\">" + flash[type][message] + "</div>");
}
}
}
I don't see what I'm missing here...
try .html_safe on the end of the string. I believe this has helped me in the past with this exact issue.
精彩评论