2013-02-14 150 views
0

在backbonejs文件已经说我们可以在loading bootstrapped modelsbackbonejs加载模型

加载自举模式所以,我也一样,因为喜欢

employeeCollection.reset(<%= @employees.to_json %>); 

但我扔了一个错误

Uncaught SyntaxError: Unexpected token & 

当我点击那个错误,它把我带到

employeeCollection.reset([{&quot;address&quot;:&quot;Madhyapur thimi 13, wamunae tole&quot;,&quot;fired&quot;:false,&quot;first_name&quot;:&quot;Gagan Shrestha&quot;,&quot;id&quot;:1,&quot;last_name&quot;:&quot;Shrestha&quot;,&quot;phone&quot;:&quot;9841971919&quot;},{&quot;address&quot;:&quot;Madhyapur thimi 13, wamunae tole&quot;,&quot;fired&quot;:true,&quot;first_name&quot;:&quot;Prem Krishna&quot;,&quot;id&quot;:2,&quot;last_name&quot;:&quot;Shrestha&quot;,&quot;phone&quot;:&quot;9841755640&quot;},{&quot;address&quot;:&quot;sdgd&quot;,&quot;fired&quot;:false,&quot;first_name&quot;:&quot;fdsfs&quot;,&quot;id&quot;:3,&quot;last_name&quot;:&quot;sfdsf&quot;,&quot;phone&quot;:&quot;5345435&quot;},{&quot;address&quot;:&quot;sdfsf&quot;,&quot;fired&quot;:true,&quot;first_name&quot;:&quot;dsfsd&quot;,&quot;id&quot;:4,&quot;last_name&quot;:&quot;fsd&quot;,&quot;phone&quot;:&quot;sfdsf&quot;},{&quot;address&quot;:&quot;Madhyapur thimi 13, wamunae tole&quot;,&quot;fired&quot;:false,&quot;first_name&quot;:&quot;Prafulla&quot;,&quot;id&quot;:5,&quot;last_name&quot;:&quot;Shrestha&quot;,&quot;phone&quot;:&quot;9841971919&quot;},{&quot;address&quot;:&quot;South africa&quot;,&quot;fired&quot;:false,&quot;first_name&quot;:&quot;ram&quot;,&quot;id&quot;:6,&quot;last_name&quot;:&quot;krishna&quot;,&quot;phone&quot;:&quot;86879979&quot;},{&quot;address&quot;:&quot;sdfdsf&quot;,&quot;fired&quot;:true,&quot;first_name&quot;:&quot;fs&quot;,&quot;id&quot;:7,&quot;last_name&quot;:&quot;sdfdsfdsf&quot;,&quot;phone&quot;:&quot;sdfdsfds&quot;},{&quot;address&quot;:&quot;juj&quot;,&quot;fired&quot;:true,&quot;first_name&quot;:&quot;rfr&quot;,&quot;id&quot;:8,&quot;last_name&quot;:&quot;hgtg&quot;,&quot;phone&quot;:&quot;9898&quot;},{&quot;address&quot;:&quot;gthy&quot;,&quot;fired&quot;:true,&quot;first_name&quot;:&quot;olo&quot;,&quot;id&quot;:9,&quot;last_name&quot;:&quot;plo&quot;,&quot;phone&quot;:&quot;6766&quot;},{&quot;address&quot;:&quot;rfrf&quot;,&quot;fired&quot;:true,&quot;first_name&quot;:&quot;uyuy&quot;,&quot;id&quot;:10,&quot;last_name&quot;:&quot;uyuy&quot;,&quot;phone&quot;:&quot;7676&quot;},{&quot;address&quot;:&quot;madhyapur thimi 12&quot;,&quot;fired&quot;:true,&quot;first_name&quot;:&quot;shuvam1&quot;,&quot;id&quot;:11,&quot;last_name&quot;:&quot;shrestha&quot;,&quot;phone&quot;:&quot;1234556778&quot;},{&quot;address&quot;:&quot;dodale&quot;,&quot;fired&quot;:true,&quot;first_name&quot;:&quot;sarita2&quot;,&quot;id&quot;:12,&quot;last_name&quot;:&quot;prajapati&quot;,&quot;phone&quot;:&quot;985757&quot;},{&quot;address&quot;:&quot;kathmandu&quot;,&quot;fired&quot;:false,&quot;first_name&quot;:&quot;hello&quot;,&quot;id&quot;:13,&quot;last_name&quot;:&quot;world&quot;,&quot;phone&quot;:&quot;0155675432&quot;}]); 

我想这个错误是由于转义JSON数据,但不是很确定。

我需要做更多的事情,所以,当页面加载,所以我不必火Ajax请求从服务器获取这些数据

感谢

+2

这可能会有帮助:[如何在Rails视图中安全地引导JSON](http://jfire.io/blog/2012/04/30/how-to-securely-bootstrap-json-in-a- rails-view /) – steveax 2013-02-14 02:18:06

+1

您的JSON正在获取HTML编码,请参阅@steveax的解决方案链接。 – 2013-02-14 02:57:30

+0

@steveax你必须写在回答部分,以便我可以接受你的答案:) – Gagan 2013-02-15 02:17:07

回答

1

Rails是HTML编码您的JSON。有几种方法可以解决这个问题,但请注意,只有使用诸如html_safe之类的东西才会产生安全隐患(XSS)。

How to Securely Bootstrap JSON in a Rails View给出了一种安全地做到这一点的好方法(作者选择覆盖json_escape)。

0

你可能需要更换我可以加载必要的数据所有&quot;用单引号"'",否则这是无效的javascript。