Reputation: 815
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.com/","description":"Stuff, you’ll need to blah blah","title":"Person detail view & engagement","url":"https://www.site.com/","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=", "comment"=>{"content"=>"https://www.site.com", "mentions"=>"https://www.site.com"}, "commit"=>"", "embed_data"=>"{\"provider_url\":\"https://www.site.com/\",\"description\":\"Stuff, you’ll need to blah blah.\",\"title\":\"Person detail view ", "engagement\",\"url\":\"https://www.site.com/\",\"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
Upvotes: 8
Views: 9702
Reputation: 150020
If 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.
Upvotes: 9
Reputation: 106027
Use encodeURIComponent
to encode the data.
settings.data += '&embed_data=' + encodeURIComponent( send_me_along );
Upvotes: 1
Reputation: 78640
Why not just set the data as an object when you call ajax
?
$.ajax(
//...
data: {
stuff: "something"...
}
);
Let jquery deal with the ampersands.
Upvotes: 1