的QueryDict总是从AJAX POST空 [英] QueryDict always empty from AJAX POST

查看:280
本文介绍了的QueryDict总是从AJAX POST空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了其中的几个问题,问,但我试图实现他们的解决方案,它并没有为我工作。

I have seen a few of these questions asked but I have tried to implement their solutions and it hasn't worked for me.

我试图发送一个基本的AJAX请求使用POST Django视图。这是我的JQuery的:

I am trying to send a basic AJAX request to a django view using POST. Here is my JQuery:

$('#save-button').click(function() {
    var names = ['BLOGGS Joe', 'SMITH John'];
    data = JSON.stringify(names);
    $.ajax({
        "contentType": "application/json; charset=utf-8",
        "data": data,
        "url" : "/ajax/myteam/save/",
        "type": "POST",
        "success": function(response) {

        }
    });
});

和这里是我的Django的看法:

And here is my Django view:

def myteam_save(request):
   if request.method == 'POST':
       if request.POST:
           print 'Hurray'
       else:
           print 'Boo'
       response = HttpResponse(json.dumps({'code':'ok'}), content_type='application/json')
       return response

当我审视发生了什么在Firebug我看到正在发的帖子,因为我打算而是从request.POST中的QueryDict对象始终是空的。

When I examine whats happening in Firebug I see that the post is being made as I intend but the QueryDict object from request.POST is always empty.

我一直小心CSRF令牌,我认为,甚至试图在我的设置,关闭django.middleware.csrf.CsrfViewMiddleware但这似乎没有任何效果。

I have been careful about csrf tokens I think and even tried turning off the 'django.middleware.csrf.CsrfViewMiddleware' in my settings but this appears to have no effect.

我是什么做错了吗?

感谢您的帮助!

推荐答案

Django的并没有真正反序列化 JSON 的有效载荷为您服务。 request.POST 是指用于HTML贴的形式,和类似物。

Django doesn't really de-serialize JSON payloads for you. request.POST is meant to be used for HTML posted forms, and the like.

有关 JSON 的有效载荷,你应该反序列化的要求自己的身体,例如: json.loads(request.body)

For JSON payloads, you should de-serialize the request body yourself, for example: json.loads(request.body).

request.body 是如何访问原始的有效载荷)。

(request.body is how you access the raw payload).

这篇关于的QueryDict总是从AJAX POST空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆