简体   繁体   English

jQuery无法解析使用Ruby-JSON构建的JSON

[英]jQuery can't parse JSON built with Ruby-JSON

I'm trying to set up an Ajax callback using jQuery, and it's just not working. 我正在尝试使用jQuery设置Ajax回调,但它不起作用。 My Ruby code looks something like this: 我的Ruby代码如下所示:

return {:one => some_html, :two => more_html}.to_json

When it gets to the client jQuery bails saying "parse error". 当到达客户端时,jQuery会说“解析错误”。 If I make it something really simple, like: 如果我使它变得非常简单,例如:

return {:one => 'Something', :two => 'Something else'}.to_json

Then it works just fine. 然后就可以了。 I guess I'm just wondering how it is that a library whose only job is to create JSON, could create invalid JSON? 我想我只是想知道一个库的唯一工作就是创建JSON,可以创建无效的JSON吗? Or is it something else? 或者是别的什么?

Is it possible the some_html & more_html has characters in it which are interfering with jQuery's parsing of the JSON? some_html&more_html是否有可能干扰jQuery解析JSON的字符? Have you used Firebug to view the AJAX response and ensure that it's valid JSON? 您是否使用Firebug查看AJAX响应并确保其为有效JSON?

You are probably not parsing it correctly, although I can't tell because you did not post the code. 您可能无法正确解析它,尽管由于您未发布代码而无法确定。 Use the JSON javascript parser to do this. 使用JSON JavaScript解析器来执行此操作。 It takes care of potential script injections, although this is usually good enough for me... 它会照顾潜在的脚本注入,尽管这通常对我来说足够好...

var json = eval(" (" + httpResponse + ") ");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM