kvadrat
kvadrat

Reputation: 135

Django jQuery Ajax. I get response, but it does not insert in html

It is enough for me with dajaxice. Now I am trying to destroy my brain using jQuery.

Earlier, I was doing $.get and all was OK. Now, when i try it with $.ajax (get) this is what i have:

urls.py

from django.conf.urls import patterns, include, url
from BitProject.views import index_page,xhr_test


urlpatterns = patterns('',
    (r'^$', index_page),
    url(r'^ajaxrequest/$', xhr_test, name='ajaxurl'),
)

views.py

from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.utils import simplejson


def index_page(request):
    return render_to_response ('template_1.html')

def xhr_test(request):
    if request.is_ajax():
        message = "Hello AJAX"
    else:
        message = "Hello"
    return HttpResponse(simplejson.dumps(message), mimetype="applicaton/json")

template_1.html

<html>
    <head>

    {% load staticfiles %}
    <link rel="stylesheet" href="{% static "css/style_2.css" %}" type="text/css">
    <script src="{% static "js/jquery_v183.js" %}"></script>

    <script>   
        $(document).ready(function(){
            $('#picture_1').click(function(){
                $.ajax({
                    url:"/BitProject/ajaxrequest/",
                    type: 'GET',
                    success: function(data){
                        $('#someEl_2').html(data)
                    },
                    dataType: JSON
                });
            });
        });


    </script>
    </head>

    <body>
        <img src="{% static "images/sunny.png" %}" id="picture_1" magrin='10px'/>
        <div id="someEl_2">First Text</div>
        </div>
    <body>
</html>

I am using chrome. I see in console that I have response - in response tab - Hello AJAX. but nothing happens on the page. where is my problem?

Upvotes: 0

Views: 1241

Answers (2)

electrocucaracha
electrocucaracha

Reputation: 131

You also have a typo in the view's code. Replace:

mimetype="applicaton/json"

for:

mimetype="application/json"

Upvotes: 0

Thierry Lam
Thierry Lam

Reputation: 46294

Can you put the dataType in javascript between quotes, change:

dataType: JSON

to

dataType: 'json'

What does your browser do when you access /BitProject/ajaxrequest/ directly?

Upvotes: 1

Related Questions