Re: ajax get json not receive data from django

398 views
Skip to first unread message

Hai Nguyen

unread,
May 15, 2013, 10:45:25 PM5/15/13
to django...@googlegroups.com


On Wednesday, May 15, 2013 11:54:43 PM UTC+7, Hai Nguyen wrote:
Hello All,

I use django 1.4 run on ubutu.
I'm trying to use jquery/ajax to display data returned from a django method.

my file views.py

from django.http import HttpResponse, Http404, HttpResponseRedirect
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.core.urlresolvers import reverse
from django.shortcuts import render_to_response
from django.core.context_processors import csrf

from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from django.contrib.auth import logout

from django.utils import simplejson
from django.core.serializers import serialize

from django.core.serializers.json import DjangoJSONEncoder
import ast

def json_page(request):
    to_json = {
        "key1" : "value1",
        "key2" : "value2"
    }
    return HttpResponse(simplejson.dumps(to_json), mimetype="application/javascript;charset=UTF-8")

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

my html file:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    </head>
    <body>
        <h1>Hello World!</h1>
        <script>
            $('document').ready(function() {
               
                var url = "http://localhost:8000/json/";
                $.ajax({
                    url: url,
                    type: "GET",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    crossDomain: false,
                    success: function(data) {
                        alert(abc);
                    },
                    error: function(xhr, textStatus) {
                        alert("doLogin\n- readyState: " + xhr.readyState + "\n- status: " + xhr.status);
                    }
                });
            });
        </script>
    </body>
</html>
-------------------------------------------------------------------------------------------

I run file html, fire bug return status = 200 (ok). But not return data

Could you help me fix it. Thanks all,






Message has been deleted

Zoltán Bege

unread,
May 16, 2013, 4:02:52 AM5/16/13
to django...@googlegroups.com
I mean HttpResponse. Sorry! :)

Hi,

You should change the mimetype on HttpResponse from mimetype="application/
javascript;charset=UTF-8" to mimetype="application/json; charset=utf-8".

Hai Nguyen

unread,
May 16, 2013, 7:53:36 AM5/16/13
to django...@googlegroups.com
Thanks you for your answer.
I'm trying to change  mimetype="application/json; charset=utf-8" but could not resolve problem

Alex Chiaranda

unread,
May 16, 2013, 11:26:44 AM5/16/13
to django...@googlegroups.com

Christian Schmitt

unread,
May 17, 2013, 5:18:18 PM5/17/13
to django...@googlegroups.com
Its better to use json instead of simplejosn : json.dumps() also csrf shouldn't be your case else you would've get an 405 error. 200 requests are fine, what does firebug says about the request? normally there would be a tab called json where you could see the values that got through the request.
Also parseJSON makes no sense since you've already got json. try something like alert(data.key1) or alert(data);
Reply all
Reply to author
Forward
0 new messages