简体   繁体   中英

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. My Ruby code looks something like this:

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

When it gets to the client jQuery bails saying "parse error". 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? 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? Have you used Firebug to view the AJAX response and ensure that it's valid 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. It takes care of potential script injections, although this is usually good enough for me...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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