Django/Ajax get request -
i'm trying implement client/server communication using django , ajax , i'm having problem ajax request.
here view:
from django.http import httpresponse rest_framework.renderers import jsonrenderer rest_framework.parsers import jsonparser taskmanager.models import task taskmanager.serializers import taskserializer class jsonresponse(httpresponse): def __init__(self, data, **kwargs): content = jsonrenderer().render(data) kwargs['content_type'] = 'application/json' super(jsonresponse, self).__init__(content, **kwargs) def task_list(request): if request.method == 'get': tasks = task.objects.all() serializer = taskserializer(tasks, many=true) return jsonresponse(serializer.data)
and jquery code:
$(document).ready(function(){ $("button").click(function(){ $.ajax({ type: "get", url: "http://127.0.0.1:8000/", datatype: "json", success: function(data){ alert(data[0]["title"]); }, error: function(){ alert("error"); } }); }); });
every time server returns status 200 ok, error function executed. when visit url directly in browser valid json output. more, if put output in file , reference in ajax call url works intended.
p.s. sorry english.
update: also, when view response details in firebug has no response body.
response headers:
server:wsgiserver/0.1 python/2.7.3 date:fri, 10 may 2013 07:56:23 gmt content-type:application/json
update: changed ajax url to: "http://127.0.0.1:8000/?callback=?"
, response body: [{"id": "518a92147c2fce152b081878", "title": "new task"}, {"id": "518a905e7c2fce1516b8f9dc", "title": "save galaxy"}, {"id": "518a904e7c2fce1516b8f9da", "title": "do barrel roll"}]
, status -- 200 , error -- parsererror.
i think there error in js content negotietion
instead of
datatype: "json",
use
contenttype: 'application/json',
or try without field
Comments
Post a Comment