Lee
Lee

Reputation: 859

No POST-Data from Ajax in Django

I want to post a array of data in AJAX to a Django-Site. Code from Javascript:

var arr = {};
arr["first"] = "first";
arr["second"] = "second";
arr["third"] = "third";
var success = ""
    var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
     var rawtext = xmlhttp.responseText;

     document.getElementById("myDiv").innerHTML= jsonToHTMLTable(rawtext);
    }
  }
xmlhttp.open("POST","/AJAX?modus=create");  
xmlhttp.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
alert(JSON.stringify(arr));
xmlhttp.send({data:JSON.stringify(arr)});

But when i debug (with Eclipse) i don't see an array or other useful fields.

This is the code from django:

def AJAX(request):
if request.method == 'POST':
    method = request.REQUEST.get("modus","")
    if method == 'create':
        test = request.POST['data']

More informations Traceback from Django-Debug

Environment:
Request Method: POST
Request URL: http://localhost:8000/AJAX?modus=create
Django Version: 1.6.5
Python Version: 3.4.1
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "C:\Python34\lib\site-packages\django\core\handlers\base.py" in get_response
  112. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Lee\web\reportingTool\ReportingTool\TempDosage\views.py" in AJAX
14. test = request.POST['data']
File "C:\Python34\lib\site-packages\django\utils\datastructures.py" in __getitem__
301.             raise MultiValueDictKeyError(repr(key))
Exception Type: MultiValueDictKeyError at /AJAX
Exception Value: "'data'"

How can I access to my data?

Upvotes: 1

Views: 1119

Answers (1)

Lee
Lee

Reputation: 859

I found the part wich was wrong (Javascript):

Here is the new JavascriptPart (which works for me):

var arr = {};
arr["first"] = "first";
arr["second"] = "second";
arr["third"] = "third";
var success = ""
$.ajax({
type: "POST",
url: "/AJAX?modus=create",
data: {"data": JSON.stringify(arr)},
success: success,
dataType: "application/json",
headers: {"X-CSRFToken": getCookie('csrftoken')}
});

And this is the django-part (view):

def AJAX(request):
  if request.method == 'POST':
      method = request.REQUEST.get("modus","")
      if method == 'create':
          data = request.POST.get('data');

Upvotes: 2

Related Questions