title: 403(Forbidden Error) while sending array of JSON objects to Django using ajax POST method
body: <p>I am getting 403(Forbidden Error) while sending array of JSON objects to Django View using ajax POST method. I have already included csrf token but still it is giving the same error. I am making 2 different ajax calls on different events and one of them is working fine while the other one gives 403 error.
  Here is my code :</p>
  
  <p>This POST request works fine.</p>
  
  <pre><code>$(document).on('submit','#dataset', function (e) {
        e.preventDefault();
  
        $.ajax({
            type:'POST',
            url:'/savedataset/',
            data:{
                lat:$('#lat').val(),
                long:$('#long').val(),
                csrfmiddlewaretoken:$('input[name=csrfmiddlewaretoken]').val()
            },
            success:function (data) {
                alert(data);
  
            },
            error:function () {
                alert("Error")
            }
        })
  
    });
  </code></pre>
  
  <p>Here is view.py for the above request :</p>
  
  <pre><code>def savedata(request):
  if request.method == 'POST':
      lat = request.POST['lat']
      long = request.POST['long']
  
      nsrpt.objects.create(
          latitude=lat,
          longitude=long
      )
  response_data = {"lat": lat, "long": long}
  return HttpResponse(json.dumps(response_data), content_type="application/json")
  </code></pre>
  
  <p>but this one returns the 403 error :</p>
  
  <pre><code>$(document).ready(function () {
        $('#Save').click(function (e) {
            $.ajax({
                type:'POST',
                url:'/savenetwork/',
                dataType:'json',
                data:{
                    networkdata: JSON.stringify(grapharr),
                    csrfmiddlewaretoken:$('input[name=csrfmiddlewaretoken]').val()
                },
                contentType:'application/json',
                success:function (data) {
                    alert(data);
                }
  
            })
  
        })
  
    })
  </code></pre>
  
  <p>Here "grapharr" is a dynamically created array which has json objects at each index and here is view.py for this POST request :</p>
  
  <pre><code>import json
  from django.core import serializers
  from django.http import HttpResponse
  from django.shortcuts import render
  
  def savenetwork(request):
  if request.method == 'POST':
      data = json.load(request.body)
  response_data = {"data" : "Test"}
  return HttpResponse(json.dumps(response_data), content_type="application/json")
  </code></pre>
  
  <p>And here is how the grapharr is created :</p>
  
  <pre><code>var grapharr = [];
    function pushtoarray2(lat, long) {
        var lati = lat;
        var longi = long;
        var node = new Object();
        node.latitude = lati;
        node.longitude = longi;
        node.connectednodes = [];
        grapharr.push(node);
    }
  </code></pre>
  
  <p>CSRF token is defined here :</p>
  
  <pre><code>&lt;form id="dataset"&gt; {% csrf_token %}
    &lt;fieldset&gt;
      &lt;p&gt;
        &lt;label&gt;Latitude&lt;/label&gt;
        &lt;input id = "lat" value = "" /&gt;
      &lt;/p&gt;
      &lt;p&gt;
        &lt;label&gt;Longitude&lt;/label&gt;
        &lt;input id = "long" value = "" /&gt;
      &lt;/p&gt;
      &lt;p&gt;
        &lt;button name="Add" value="Add" type="button" onclick="addmarker()"&gt;Add&lt;/button&gt;
      &lt;/p&gt;
      &lt;select id="myselect" multiple size="5"&gt;
      &lt;/select&gt;
      &lt;p&gt;
        &lt;button name="Connect" value="Connect" type="button" onclick="getSelectValues()"&gt;Connect&lt;/button&gt;
      &lt;/p&gt;
      &lt;p&gt;
        &lt;button name="Delete" value="Delete" type="button" onclick="deletelatlong()"&gt;Delete&lt;/button&gt;
      &lt;/p&gt;
        &lt;input type="submit" value="Submit"&gt;
        &lt;p&gt;
        &lt;button id="fetch" name="fetch" value="fetch" type="button"&gt;Fetch&lt;/button&gt;
      &lt;/p&gt;
  
    &lt;/fieldset&gt;
      &lt;div id="map"&gt;&lt;/div&gt;
      &lt;p&gt;
        &lt;button id="Save" name="Save" value="Save" type="button"&gt;Save Network&lt;/button&gt;
      &lt;/p&gt;
      &lt;p&gt;
        &lt;button name="Clear" value="Clear" type="button" onclick="printArray()"&gt;Clear Map&lt;/button&gt;
      &lt;/p&gt;
  </code></pre>
  
  <p></p>
  
  <p>Here is the url.py</p>
  
  <pre><code>from django.conf.urls import url
  from networkeditor import views
  urlpatterns = [
  url(r'^$', views.index, name = 'index'),
  url(r'^savedataset/$', views.savedata),
  url(r'^fetchdata/$', views.fetchdata),
  url(r'^savenetwork/$', views.savenetwork),
  ]
  </code></pre>
  
  <p>I tried searching a lot but everywhere it was mentioned to include csrf token which is already there. Please help me out in finding what I am missing here. I am a novice to django/ajax.</p>