tags:

views:

59

answers:

1

I've got a setup where I use the JQuery Form plugin to submit my forms through ajax, returning a json object from the server (running django, using simplejson.dumps). The json returned is constructed as this:

status: [success/invalid/error] error: [errortext if any] html: [html-text to be inserted]

My problem is that when entering the success function of my ajax call, the html part of the json is escaped so that the results I get are of the type <strong>Hello<\strong>. I've doublechecked the json string produced by the server, it validates using json-lint and has unescaped html-entities. Any suggestions on how I stop JQuery from escaping my html?

+1  A: 

Make sure you're using .html() to use that response and not .text(), which will escape it like you're talking about.

Nick Craver
I was actually trying both .replaceWith() and .html() but no luck and if I try to console.log the full json, it seems like it's already escaped.
Jens Alm
@Jens - I'm confused...your question says "it validates using json-lint and has unescaped html-entities" but now you're saying it *is* escaped? If that's the case it's a server-side issue, you should prevent the escaping there.
Nick Craver
@Nick - Yes, I'm confused as well. The json that my server returns is bona-fide, valid json with unescaped html-fragments inside, however, by the time that jQuery has transformed the json string into a javascript object, the html inside the json has been escaped, replacing < with < and so on.
Jens Alm
@Jens - What does firebug show in the net panel?
Nick Craver
@Nick - Well, actually I moved on to another solution, generating the html on the client side instead, but I will probably run into it again, I'll check again then, thanks for the help!
Jens Alm
@Nick: found the problem now in another spot. I forgot to mark_safe html that I was sending through a template. It was soppy debugging on my part where I tried to change several things at one time to find the problem that made me believe that it was a client-side issue.
Jens Alm