[Answer]-How do I get JQuery/AJAX results to work with render_to_response

1👍

function ajaxTest() { 
    $.ajax({
        type: 'POST',
        url: '/trivia/ajax_test/',
        data: {
              recipient: document.getElementById("id_recipient").value,
              message: document.getElementById("id_message").value
              },
        success: function(result){
            var html = $(result);
            $("#messageTable").find("tbody").clear().append('<tr><td>' + html + '</td></tr>');
        } 
    });

    return false;
}


@csrf_exempt
def ajax_test(request):
    if request.is_ajax():
        //other codes here

        return render_to_response('trivia_ajax_messages.html', {
            'ajax_messages': message_list
        }, context_instance=RequestContext(request))

    return HttpResponse("Something went wrong")

Leave a comment