最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to safely use JSON.stringify - Stack Overflow

programmeradmin4浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 9

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.

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 );
发布评论

评论列表(0)

  1. 暂无评论