5

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 error - JSON.parse: expected ',' or '}' after property value in object when i do jQuery.parseJSON()

Have tried the following:

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

None are working. This is really killing me!

Thanks in advance.

Update : The 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>'}
ZX12R
  • 4,760
  • 8
  • 38
  • 53
  • Hmm, need more context. This works for me: `$.parseJSON("{\"a\":\"
    asd
    \"}")`. Can you show the exact text that's being passed to `$.parseJSON`, pulled from the AJAX call or HTML page, not from the REPL?
    – s4y Aug 24 '11 at 02:50
  • The exact text parsed is a bit large. But my senses say that there isn;t issue with the parsing but with the way json is being rendered, as the parsing didn't cause errors in rails2.3.2 – ZX12R Aug 24 '11 at 04:47
  • I agree, but I wonder whether the problem is not with a change in the JSON encoder itself, but a problem later on with how the JSON string is handled (e.g. it's being encoded for HTML, which it shouldn't be). If the JSON encoder changed, it might reveal another problem like this. – s4y Aug 24 '11 at 18:04
  • Could you look at the sting that's ending up in `$.parseJSON`'s hands? How's it being loaded: is it included inline in a script, being loaded asynchronously from another file, or something else? – s4y Aug 24 '11 at 18:06
  • the json comes as a response for an ajax request. not inline. has the way to render json changed in rails 3? i feel i am missing something very basic here – ZX12R Aug 25 '11 at 04:32
  • Could you please post the code that issues & responds to the ajax request? – bonkydog Sep 04 '11 at 18:57
  • @ZX12R : Are you still having this issue? If so, could you post your Controller who is handling the request? – Fishz Sep 08 '11 at 01:06
  • @fishz: by controller you mean the action method or the entire controller? – ZX12R Sep 08 '11 at 12:13

0 Answers0