On Tue, Jul 2, 2013 at 4:49 PM, Sanjay Bhangar <sanjaybhan...@gmail.com> wrote:
> On Tue, Jul 2, 2013 at 3:37 PM, Larry Martell <larry.mart...@gmail.com>
> wrote:
>>
>> On Tue, Jul 2, 2013 at 4:24 PM, Sithembewena Lloyd Dube
>> <zebr...@gmail.com> wrote:
>> > What data format is your view returning? Django views return HTTP
>> > response
>> > objects (by default, at least) - although Python lists (and other Python
>> > collections?) should work when passed into a view in its context (I have
>> > found Django querysets to be an exception).
>> >
>> > AJAX is Asynchronous Javascript, so I presume that an AJAX call would
>> > expect
>> > something like JSON output from a callable. I guess with some pain one
>> > could
>> > get Javascript to ingest Django querysets? I couldn't think of a
>> > sensible
>> > reason to do so - and I stand to be corrected.
>>
>> I want to return a string. I've never done this before. I've been
>> reading examples on the web and I came up with this:
>>
>> $.ajax({
>>     url: 'permalink/',
>>     type: 'GET',
>>     data: {
>>         report: "{% url motor.core.reports.views.view
>> the_controller.app.name the_controller.get_name %}",
>>         params: "{{ the_report.params_string }}"
>>     },
>>     dataType: 'json',
>>     success: function (data) {
>>         setTimeout(function () {
>>             alert(data);
>>         }, 0);
>>     }
>> });
>>
>
> You shouldn't need the setTimeout in your success call - just function(data)
> { alert(data); } .. should be okay.
>
>> In my function I first tried returning just the string, but that was
>> causing a 500 error. Then I tried this:
>>
>> return HttpResponse(content=data, content_type="text/plain", status=201)
>>
> try status=200 ?
>
>>
>> I don't get the error, but the ajax success function does not seem to be
>> called.
>>
> An invaluable tool here is to use the Developer Tools in the browser to
> inspect AJAX requests made and their responses .. in Chrome, open Developer
> Tools (f12 is a handy shortcut for this) and go to the Network Tab, and then
> you should see the Network Requests show up, and be able to examine what was
> returned, etc..

In the browser the response has this:

HTTP/1.0 200 OK
Date: Tue, 02 Jul 2013 22:53:47 GMT
Server: WSGIServer/0.1 Python/2.7.2
Vary: Cookie
Content-Type: text/plain

No content. But I verified from pdb that the python code is sending
something in the content when it does this:

return HttpResponse(content=data, content_type="text/plain", status=200)

>> > On Wed, Jul 3, 2013 at 12:06 AM, Larry Martell <larry.mart...@gmail.com>
>> > wrote:
>> >>
>> >> I'm invoking a view from an ajax call and I want to return data. But
>> >> the browser interpertates the return as a 500 (internal sever error)
>> >> and my ajax success function does not get called. How can I return a
>> >> successful status (like a 201) and also return data?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to