2

I have a simple ajax post request. The request is sent. But why when I print request.POST in views.py, it says that: request.POST: <QueryDict: {}>.

if (start <= end) {
   // fire a request to /tryit
       var args = {
       type: "POST",
       url: "/tryit/",
       data: {'x':x1, 'y': y1},
       error: function() {
          console.log("Error occurs");
       },
       success: function() {
          console.log("save_modification works");
       },
       complete:done
    };
    $.ajax(args);
    alert("ajax request is sent");
} 

In views.py,

def tryit(request):
    print "request: ", request

Here is the output when I print request information. The output is included as below.

[Wed Nov 14 14:50:44 2012] [error] post request:  <WSGIRequest
[Wed Nov 14 14:50:44 2012] [error] path:/tryit/,
[Wed Nov 14 14:50:44 2012] [error] GET:<QueryDict: {}>,
[Wed Nov 14 14:50:44 2012] [error] POST:<QueryDict: {}>,
[Wed Nov 14 14:50:44 2012] [error] COOKIES:{'sessionid': 'f997a4c4ba0f576fe11141e0c0dcd38
 d'},
[Wed Nov 14 14:50:44 2012] [error] META:{'CONTENT_LENGTH': '128',
[Wed Nov 14 14:50:44 2012] [error]  'CONTENT_TYPE': 'application/x-www-form-urlencoded',
[Wed Nov 14 14:50:44 2012] [error]  'DOCUMENT_ROOT': '/var/www',
[Wed Nov 14 14:50:44 2012] [error]  'GATEWAY_INTERFACE': 'CGI/1.1',
[Wed Nov 14 14:50:44 2012] [error]  'HTTP_ACCEPT': '*/*',
[Wed Nov 14 14:50:44 2012] [error]  'HTTP_ACCEPT_ENCODING': 'gzip, deflate',
[Wed Nov 14 14:50:44 2012] [error]  'HTTP_ACCEPT_LANGUAGE': 'en-ca',
[Wed Nov 14 14:50:44 2012] [error]  'HTTP_CONNECTION': 'Keep-Alive',
[Wed Nov 14 14:50:44 2012] [error]  'HTTP_COOKIE': 'sessionid=f997a4c4ba0f576fe11141e0c0d
cd38d',
[Wed Nov 14 14:50:44 2012] [error]  'HTTP_PRAGMA': 'no-cache',

[Wed Nov 14 14:50:44 2012] [error]  'HTTP_USER_AGENT': 'Mozilla/5.0 (compatible; MSIE 9.0
; Windows NT 6.0; Trident/5.0)',
[Wed Nov 14 14:50:44 2012] [error]  'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest',
[Wed Nov 14 14:50:44 2012] [error]  'PATH_INFO': u'/tryit/',
[Wed Nov 14 14:50:44 2012] [error]  'PATH_TRANSLATED': 'django.wsgi/tryit/',
[Wed Nov 14 14:50:44 2012] [error]  'QUERY_STRING': '',

[Wed Nov 14 14:50:44 2012] [error]  'REQUEST_METHOD': 'POST',
[Wed Nov 14 14:50:44 2012] [error]  'REQUEST_URI': '/tryit/',
[Wed Nov 14 14:50:44 2012] [error]  'SCRIPT_FILENAME': 'django.wsgi',
[Wed Nov 14 14:50:44 2012] [error]  'SCRIPT_NAME': u'',

[Wed Nov 14 14:50:44 2012] [error]  'SERVER_ADMIN': 'webmaster@localhost',

[Wed Nov 14 14:50:44 2012] [error]  'SERVER_PORT': '80',
[Wed Nov 14 14:50:44 2012] [error]  'SERVER_PROTOCOL': 'HTTP/1.1',
[Wed Nov 14 14:50:44 2012] [error]  'SERVER_SIGNATURE': '<address>Apache/2.2.22 (Ubuntu)
[Wed Nov 14 14:50:44 2012] [error]  'SERVER_SOFTWARE': 'Apache/2.2.22 (Ubuntu)',

[Wed Nov 14 14:50:44 2012] [error]  'mod_wsgi.callable_object': 'application',
[Wed Nov 14 14:50:44 2012] [error]  'mod_wsgi.enable_sendfile': '0',
[Wed Nov 14 14:50:44 2012] [error]  'mod_wsgi.handler_script': '',
[Wed Nov 14 14:50:44 2012] [error]  'mod_wsgi.input_chunked': '0',
[Wed Nov 14 14:50:44 2012] [error]  'mod_wsgi.listener_host': '',
[Wed Nov 14 14:50:44 2012] [error]  'mod_wsgi.listener_port': '80',
[Wed Nov 14 14:50:44 2012] [error]  'mod_wsgi.process_group': '',
[Wed Nov 14 14:50:44 2012] [error]  'mod_wsgi.queue_start': '1352926244930263',
[Wed Nov 14 14:50:44 2012] [error]  'mod_wsgi.request_handler': 'wsgi-script',
[Wed Nov 14 14:50:44 2012] [error]  'mod_wsgi.script_reloading': '1',
[Wed Nov 14 14:50:44 2012] [error]  'mod_wsgi.version': (3, 4),
[Wed Nov 14 14:50:44 2012] [error]  'wsgi.errors': <mod_wsgi.Log object at 0xa8224c78>,
[Wed Nov 14 14:50:44 2012] [error]  'wsgi.file_wrapper': <built-in method file_wrapper of
 mod_wsgi.Adapter object at 0xa8210218>,
[Wed Nov 14 14:50:44 2012] [error]  'wsgi.input': <mod_wsgi.Input object at 0xa8205ef8>,
[Wed Nov 14 14:50:44 2012] [error]  'wsgi.multiprocess': True,
[Wed Nov 14 14:50:44 2012] [error]  'wsgi.multithread': True,
[Wed Nov 14 14:50:44 2012] [error]  'wsgi.run_once': False,
[Wed Nov 14 14:50:44 2012] [error]  'wsgi.url_scheme': 'http',
[Wed Nov 14 14:50:44 2012] [error]  'wsgi.version': (1, 0)}>


But when I use fiddler, I can see the query parameter string. I am stuck here for a day. Please help. Thanks.

New updates: I use chrome and in the Network tab, it says there is an internal server error and the initiator is jquery.min.js:16. I do not undersand this information. Does this contain some information in it?

If I change from POST to GET, the query parameters can be recieved by the server. But if I use POST, the query parameters cannot be posted. How can explain it?

susanna
  • 1,395
  • 3
  • 20
  • 32
  • Did you try looking at this in Firebug or similar to see what is actually being posted? Are you sure `mydata` actually contains data? – ernie Nov 14 '12 at 18:34
  • In chrome developer tools go to the console tab and fire the event your trying to test. Do any errors show up? If so post the errors. – Andrew Kloos Nov 14 '12 at 21:45
  • Your code is too incomplete for us to make a decent suggestion. Give us all of the code required to test with and we can give you better advice. – Andrew Kloos Nov 15 '12 at 02:14

3 Answers3

1

You need to use your console and inspect both content and headers of your ajax calls.

You do this in firebug (I hope that you have firebug installed in your firefox), or in Chrome right click &inspect element.

There in the opened window you should have NET panel (firebug), or Network in Chrome.

There you will see details of every call made to server, and it's response.

tonino.j
  • 3,837
  • 28
  • 27
0

data should be a dict. Your js should look like this:

if (start <= end) {
   // fire a request to /tryit
   var args = {
      ... 
      data: {"mydata" : mydata },
      ...
} 
Melevir
  • 340
  • 3
  • 10
  • Hm.. [docs](http://api.jquery.com/jQuery.ajax/) says, that transferred data is converted to string, so it should be JSON objects. change `mydata` to `{'x': x1, 'y': y1, 'z': z1}`. That should work. – Melevir Nov 14 '12 at 19:48
0

When debugging the javascript in firebug I get the error "x1 is not defined".

Do you intend x1 and y1 to be strings? If so try this...

data: {'x':'x1', 'y': 'y1'},

Hope this helps,

Andrew

Andrew Kloos
  • 4,189
  • 4
  • 28
  • 36