Example of using JavaScript AJAX XmlHttpRequest with Django JsonResponse view

Forget Axios or any other third-party JavaScript library pertaining to API calling, the purpose of this article is to explain how to utilize the basic XmlHttpRequest with your Django project.

  1. Begin by setting up your Django project:

        mkdir ajax_django_example
        cd ajax_django_example/
        virtualenv env
        source env/bin/activate
        python --version
        pip install django
        django-admin startproject example
        cd example
        python manage.py startapp hello
        python manage.py runserver
  2. Populate your views.py file:

        # hello/views.py
        from django.http import HttpResponse
        from django.shortcuts import render
        from django.http import JsonResponse
        def index(request):
            return render(request, 'hello/index.html', {
        def get_version(request):
            return JsonResponse({'version': '0.1.0-beta'})
        def post_add(request):
            a = request.POST.get("a")
            b = request.POST.get("b")
            a = float(a)
            b = float(b)
            c = a + b
            return JsonResponse({'result': c})
        # Would you like to know more?
        # https://docs.djangoproject.com/en/2.2/ref/request-response/#jsonresponse-objects
  3. Set the urls.py file:

        # hello/urls.py
        from django.urls import path
        from . import views
        urlpatterns = [
            path('', views.index, name='index'),
            path('version', views.get_version, name='version'),
            path('add', views.post_add, name='add'),
  4. Set the project’s settings.py file:

        # example/settings.py
        # ...
        INSTALLED_APPS = [
        MIDDLEWARE = [
            # 'django.middleware.csrf.CsrfViewMiddleware',
        # ...
  5. Set the project’s urls.py file:

        # example/urls.py
        """example URL Configuration
        The `urlpatterns` list routes URLs to views. For more information please see:
        Function views
            1. Add an import:  from my_app import views
            2. Add a URL to urlpatterns:  path('', views.home, name='home')
        Class-based views
            1. Add an import:  from other_app.views import Home
            2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
        Including another URLconf
            1. Import the include() function: from django.urls import include, path
            2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
        from django.contrib import admin
        from django.urls import path, include
        urlpatterns = [
            path('admin/', admin.site.urls),
            path('', include('hello.urls')),
  6. Next step is the JavaScript portion.

        // hello/templates/hello/index.js
        function getVersion() {
            var xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function() {
                if (this.readyState == 4 && this.status == 200) {
                    document.getElementById('demoGet').innerHTML = this.responseText;
            xhttp.open('GET', 'version', true);
        function postAdd() {
            var xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function() {
                if (this.readyState == 4 && this.status == 200) {
                    document.getElementById('demoPost').innerHTML = this.responseText;
            xhttp.open('POST', 'add', true);
            xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        // Would you like to know more?
        // https://www.w3schools.com/js/js_ajax_intro.asp
  7. Continue

        <!-- hello/templates/hello/index.html -->
        <!DOCTYPE html>
                <h1>Using XMLHttpRequest Object and Django JsonResponse</h1>
                <h2>GET Example</h2>
                <p id="demoGet">Let AJAX change this text.</p>
                <button type="button" onclick="getVersion()">Run GET</button>
                <h2>POST Example</h2>
                <p id="demoPost">Let AJAX change this text.</p>
                <button type="button" onclick="postAdd()">Run POST</button>
                    {% include "hello/index.js" %}
  8. That’s it! Test it out the code and you should see it working.

Cover photo by Joan Gamell on Unsplash.