javascript - JSON string created in Rails replaces whitespace with plus sign (+) -



javascript - JSON string created in Rails replaces whitespace with plus sign (+) -

rails:

object = object.find_by(params[:id]) object_items = { "1" => { :id => "123456", :name => "pancakes yum!" }, "2" => { :id => "789010", :name => "hello 123" }} cookies[:something] = { "id" => object.id, "title" => object.title, "object_items" => object_items }.to_json

let's object.title produces string "hello world"

cookie content:

%7b%22id%22%3a2%2c%22title%22%3a%22hello+world%22%2c%22object_items%22%3a%7b%221%22%3a%7b%22id%22%3a%22123456%22%2c%22name%22%3a%22pancakes+yum!%22%7d%2c%222%22%3a%7b%22id%22%3a%22789010%22%2c%22name%22%3a%22hello+123%22%7d%7d%7d

problem:

the json string beingness created replaces/escapes whitespace plus sign (+) instead of %20 means if read string , grab value object.title in html/javascript read "hello+world" , not "hello world" expected.

all other characters seem replaced/escaped - because space exists within double quote? can't figure out why it's producing string is.

i don't understand why communicating client via cookies. why not utilize controller action , ajax request?

class somethingcontroller def show object = object.find_by(params[:id]) object_items = { "1" => { :id => "123456", :name => "pancakes yum!" }, "2" => { :id => "789010", :name => "hello 123" }} render :json => { "id" => object.id, "title" => object.title, "object_items" => object_items } end

then request using jquery or this:

$.get('/something/1.json', function(results) { alert(results); });

what's point in using rails if you're not going utilize rails?

cookies cgi escaped before beingness sent client. when client retransmits them rails unescapes them.

you can test behaviour this:

rails console c cgi.escape("something something") => "something+something" cgi.unescape("something+something") => "something something"

javascript ruby-on-rails json cookies whitespace

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -