一个简单的ajax请求发布为空

时间:2012-11-14 18:14:59

标签: jquery python ajax django

我有一个简单的ajax帖子请求。请求已发送。但是为什么当我在views.py中打印request.POST时,它会说: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");
} 

在views.py中,

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

这是打印请求信息时的输出。输出如下:

[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.

新更新:  我使用chrome,在网络选项卡中,它表示存在内部服务器错误,启动器为jquery.min.js:16。我不会透露这些信息。这包含一些信息吗?

如果我从POST更改为GET,服务器可以接收查询参数。但是如果我使用POST,则无法发布查询参数。怎么解释呢?

3 个答案:

答案 0 :(得分:1)

您需要使用控制台并检查ajax调用的内容和标题。

你在firebug中这样做(我希望你的firefox中安装了firebug), 或在Chrome中右击并检查元素。

在打开的窗口中你应该有NET面板(firebug), 或Chrome中的网络。

在那里,您将看到每次拨打服务器的详细信息及其响应。

答案 1 :(得分:0)

data应该是一个词典。你的js应该是这样的:

if (start <= end) {
   // fire a request to /tryit
   var args = {
      ... 
      data: {"mydata" : mydata },
      ...
} 

答案 2 :(得分:0)

在firebug中调试javascript时,我收到错误“x1未定义”。

你打算将x1和y1作为字符串吗?如果是这样试试......

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

希望这有帮助,

安德鲁