tags:

views:

131

answers:

1

Hi,

I'm trying to figure out how to pass html back to a jQuery call, but for some reason my code refuses to work.

t = get_template('success.html')
html = t.render(Context({'success': True}))    # should render '<p><h1>aoeu</h1></p>'
x = "{'success' : '" + html + "'}"
return HttpResponse(x)

jQuery code:

$.post("adduser", data, function(responseData){
    $('#content').html(responseData.success);
}, "json");

if I replace html with a literal string (just 'asdf' or something), #content changes correctly. If I use the rendered html, it doesn't show. What am I doing wrong? Also, is this the correct way to make a dynamic call with django?

+2  A: 

You are using the data type 'json' you need to use 'html' instead. (source)

$.post("adduser", data, function(responseData){
    $('#content').html(responseData.success);
}, "html");
Nadia Alramli
I changed it to html, but if do console.log(responseData.success)I get undefined. Should I be sending back plain text/html? Or in JSON format? Neither seem to be working for me.
victor
Actually, nevermind. I didn't take out the '.success' after responseData. Thanks!
victor