Phil Kulak
Phil Kulak

Reputation: 7230

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?

Upvotes: 0

Views: 1211

Answers (2)

Josh Stodola
Josh Stodola

Reputation: 82523

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 + ") ");

Upvotes: 0

Jason Berry
Jason Berry

Reputation: 2479

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?

Upvotes: 2

Related Questions