akshayslodha
akshayslodha

Reputation: 37

django-ajax json response

I have a django url : '127.0.0.1:8000/showsym' mapped to view returning json response

def get_symptoms(request):
    bp=BodySubPart.objects.get(body_subpart="head")
    data1=bp.symptoms.all()
    data = serializers.serialize('json', data1)
    return HttpResponse(data,mimetype='application/json')

now i am trying to parse this in ajx_form.html and code for that is :

<html>
<head>
<title>Hist</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"     type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
(function() {
$.get('127.0.0.1:8000/showsym/', function(data1) {
       alert(data1);
    });
});

</script>

</body>
</html>

but it is not giving me any output the page is coming blank

please help me here somebody

Upvotes: 0

Views: 2039

Answers (1)

jpic
jpic

Reputation: 33410

It is because your code tries to get the url: /127.0.0.1:8000/showsym/

Change 127.0.0.1:8000/showsym/ to /showsym/.

I suggest you use $.getJSON and name urls, assuming the url name of /showsym is showsym:

$(document).ready(function() {
    $.getJSON('{% url showsym %}', function(data, textStatus, jqXHR) {
        alert(data);
    })
})

Upvotes: 1

Related Questions