我得到一个JSON对象,然后我将其字符串化为var embed。 console.log看起来像:
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"}
然后在ajax beforeSend中我尝试传递这个:
settings.data += '&embed_data=' + send_me_along;
这是它破裂的地方。我不知道为什么。你呢? send_me_along中断的东西,JSON对象永远不会成为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"}
好像&在标题中弄乱了帖子。当使用settings.data不允许字符串化数据破坏所有东西时,有什么东西需要用jQuery完成吗?
由于
答案 0 :(得分:7)
如果您尝试将一串JSON作为url参数传递,则需要对其进行编码,以便在url(如&符号)中具有意义的特殊字符不会破坏事物。如下所示:
settings.data += '&embed_data=' + encodeURIComponent(send_me_along)
encodeURIComponent()
MDN的更多信息。
答案 1 :(得分:1)
使用encodeURIComponent
对数据进行编码。
settings.data += '&embed_data=' + encodeURIComponent( send_me_along );
答案 2 :(得分:0)
为什么不在调用ajax
时将数据设置为对象?
$.ajax(
//...
data: {
stuff: "something"...
}
);
让jquery处理&符号。