asd'}.to_js...">

Rails html coding in json issue

Consider the following two scenarios:

Ruby 1.8.7 Rails 2.3.2

{:a => '<div style="color:red;">asd</div>'}.to_json ?> {"a":"\u003div style=\"color:red;\"\u003Easd\u003/div\u003E"} 

this does not cause any problems when I do jQuery.parseJSON ()

Ruby 1.8.7 Rails 3.0.9

 {:a => '<div style="color:red;">asd</div>'}.to_json ?> {"a":"<div style=\"color:red;\">asd</div>"} 

this causes an error - JSON.parse: expected ',' or '}' after the property value in the object when I do jQuery.parseJSON ()

Have tried the following:

  • :: Iconv.conv ('UTF-8 // IGNORE', 'UTF-8', self + '') [0 ..- 2]
  • .html safe
  • JSON.encode

No. It really kills me!

Thanks in advance.

Update: json renderer

 render :text => {:success => true, :replacement => '<div style="color:red;">asd</div>'}.to_json 

also tried

 render :json => {:success => true, :replacement => '<div style="color:red;">asd</div>'} 
+4
source share
1 answer

Run the C # encoding file: UTF-8 comment.

Try using the .force_encoding ('UTF-8') method.

For 1.8.7 see String.force_encoding () in Ruby 1.8.7 (or Rails 2.x)

0
source

All Articles