0

I'm very confused with my code. Here is the ajax:

<script type="text/javascript">
    jQuery(document).ready(function() {
        $("#search-filter").click(function (evt) {
            evt.preventDefault();
            q=$('#q').val();
            q_fields=$('#q-fields').attr('value');
            $.ajax({
                type: 'GET',
                url: $(this).attr("href"),
                data: {'q': q, 'q_fields': q_fields},
                success: function(data) {
                    var returned_html = $(data);
                    alert(returned_html);
                }
            })
        });
    });
</script>

My view returns a HTML that I can print. It's as expected.

if self.request.is_ajax():
    return render_to_response(self.obj_template, {'object_list': res}, context_instance=RequestContext(self.request))

I can't get the return in my var returned_html. I should have miss something. But what? The response from firebug is object of type 'HttpResponse' has no len().

EDIT:

Here is the template: self.obj_template I use to build my piece of HTML.

{% for object in object_list %}
    <li>object.name</li>
{% endfor %}

And the print the return of my view I'd like to retrieve:

print render_to_response(self.obj_template, {'object_list': res}, context_instance=RequestContext(self.request))


<tr class="clickableRow" href="/contacts/1054/">
    <td>ABBA</td>
    <td>toto</td>
    <td>[email protected]</td>
    <td>CAM SA</td>
</tr>

<tr class="clickableRow" href="/contacts/1045/">
    <td>toto</td>
    <td>toto</td>
    <td></td>
    <td>None</td>
</tr>
3
  • What is the type/value of res? Dec 6, 2014 at 19:17
  • @OozeMeister it's a queryset as object.objects.all() Dec 6, 2014 at 20:46
  • When you print the render_to_response what does the console show? Dec 8, 2014 at 20:47

2 Answers 2

0

It's your js that was wrong, how about try this:

var returned_html = data;
1
  • THanks, you were right on that error. But my problem came from the use of a class base view to return a piece of HTML, which is not what a CBS is for. Dec 13, 2014 at 14:42
0

I gave too few information on this topic to be helped and I'm sorry about that. My error consisted in the use of a CBV to respond a formated HTML without considering that the CBV render a whole page. So I had to call a function outside the CBV to return the exact code as expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.