views:

487

answers:

2

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?

+2  A: 

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?

Jason Berry
Actually, it looks like Ramaze is unescaping the quotes in my JSON, which is breaking everything.
That'll do it :)
Jason Berry
Got it! You need to use "respond('text')" to get Ramaze to not use a template. I'm not sure what to do with this question now, since it's pretty worthless now...
Probably mark it as answered then close it.
Jason Berry
A: 

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