I get a JSON object, which I then stringify to var embed. The console.log looks like:
console.log(send_me_along)
{"provider_url":"/","description":"Stuff, you’ll need to blah blah","title":"Person detail view & engagement","url":"/","version":"1.0","provider_name":"site","type":"link"}
Then in ajax beforeSend I try to pass this along:
settings.data += '&embed_data=' + send_me_along;
This is where it breaks. I don't know why. Do you? Something send_me_along breaks and the JSON object never makes it to rails.
Started POST "/st" for 127.0.0.1 at 2012-01-12 17:20:25 -0800
Parameters: {"utf8"=>"✓", "authenticity_token"=>"MzDImoksi56IZ1Fa4ldM8jaFyBy61xaWt4bf3z0/3UQ=", "ment"=>{"content"=>"", "mentions"=>""}, "mit"=>"", "embed_data"=>"{\"provider_url\":\"/\",\"description\":\"Stuff, you’ll need to blah blah.\",\"title\":\"Person detail view ", "engagement\",\"url\":\"/\",\"version\":\"1.0\",\"provider_name\":\"site\",\"type\":\"link\"}"=>nil, "id"=>"ae86c5b7a6"}
It appears as if the & in the title is messing up on the post. is there something that needs to be done w jQuery when using settings.data to not allow the stringified data to break everything?
Thanks
I get a JSON object, which I then stringify to var embed. The console.log looks like:
console.log(send_me_along)
{"provider_url":"https://www.site./","description":"Stuff, you’ll need to blah blah","title":"Person detail view & engagement","url":"https://www.site./","version":"1.0","provider_name":"site","type":"link"}
Then in ajax beforeSend I try to pass this along:
settings.data += '&embed_data=' + send_me_along;
This is where it breaks. I don't know why. Do you? Something send_me_along breaks and the JSON object never makes it to rails.
Started POST "/st" for 127.0.0.1 at 2012-01-12 17:20:25 -0800
Parameters: {"utf8"=>"✓", "authenticity_token"=>"MzDImoksi56IZ1Fa4ldM8jaFyBy61xaWt4bf3z0/3UQ=", "ment"=>{"content"=>"https://www.site.", "mentions"=>"https://www.site."}, "mit"=>"", "embed_data"=>"{\"provider_url\":\"https://www.site./\",\"description\":\"Stuff, you’ll need to blah blah.\",\"title\":\"Person detail view ", "engagement\",\"url\":\"https://www.site./\",\"version\":\"1.0\",\"provider_name\":\"site\",\"type\":\"link\"}"=>nil, "id"=>"ae86c5b7a6"}
It appears as if the & in the title is messing up on the post. is there something that needs to be done w jQuery when using settings.data to not allow the stringified data to break everything?
Thanks
Share Improve this question asked Jan 13, 2012 at 1:31 Rachela MeadowsRachela Meadows 8153 gold badges12 silver badges20 bronze badges 1-
send_me_along is an object, not sure what you want to append to the
settings.data
, did you want a query string? – Ya Zhuang Commented Jan 13, 2012 at 1:50
3 Answers
Reset to default 9If you're trying to pass a string of JSON as a url parameter you need to encode it so that special characters that have meaning in a url (like ampersands) will not break things. So something like:
settings.data += '&embed_data=' + encodeURIComponent(send_me_along)
More info on encodeURIComponent()
at MDN.
Why not just set the data as an object when you call ajax
?
$.ajax(
//...
data: {
stuff: "something"...
}
);
Let jquery deal with the ampersands.
Use encodeURIComponent
to encode the data.
settings.data += '&embed_data=' + encodeURIComponent( send_me_along );