我的传入Django请求中的JSON数据在哪里?

问题描述 投票:134回答:12

我正在尝试使用Django / Python处理传入的JSON / Ajax请求。

request.is_ajax()True的请求,但我不知道有效载荷在哪里与JSON数据。

request.POST.dir包含:

['__class__', '__cmp__', '__contains__', '__copy__', '__deepcopy__', '__delattr__',
 '__delitem__', '__dict__', '__doc__', '__eq__', '__ge__', '__getattribute__',
'__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__',
 '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', 
'__setattr__', '__setitem__', '__str__', '__weakref__', '_assert_mutable', '_encoding', 
'_get_encoding', '_mutable', '_set_encoding', 'appendlist', 'clear', 'copy', 'encoding', 
'fromkeys', 'get', 'getlist', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 
'keys', 'lists', 'pop', 'popitem', 'setdefault', 'setlist', 'setlistdefault', 'update', 
'urlencode', 'values']

请求帖子键中显然没有键。

当我查看Firebug中的POST时,请求中会发送JSON数据。

python ajax json django content-type
12个回答
214
投票

如果你把JSON发布到Django,我想你想要request.body(Django上的request.raw_post_data <1.4)。这将为您提供通过帖子发送的原始JSON数据。从那里你可以进一步处理它。

以下是使用JavaScript,jQuery,jquery-json和Django的示例。

JavaScript的:

var myEvent = {id: calEvent.id, start: calEvent.start, end: calEvent.end,
               allDay: calEvent.allDay };
$.ajax({
    url: '/event/save-json/',
    type: 'POST',
    contentType: 'application/json; charset=utf-8',
    data: $.toJSON(myEvent),
    dataType: 'text',
    success: function(result) {
        alert(result.Result);
    }
});

Django的:

def save_events_json(request):
    if request.is_ajax():
        if request.method == 'POST':
            print 'Raw Data: "%s"' % request.body   
    return HttpResponse("OK")

Django <1.4:

  def save_events_json(request):
    if request.is_ajax():
        if request.method == 'POST':
            print 'Raw Data: "%s"' % request.raw_post_data
    return HttpResponse("OK")

2
投票
html code 

file name  : view.html


    <!DOCTYPE html>
    <html>
    <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
        $("#mySelect").change(function(){
            selected = $("#mySelect option:selected").text()
            $.ajax({
                type: 'POST',
                dataType: 'json',
                contentType: 'application/json; charset=utf-8',
                url: '/view/',
                data: {
                       'fruit': selected
                      },
                success: function(result) {
                        document.write(result)
                        }
        });
      });
    });
    </script>
    </head>
    <body>

    <form>
        <br>
    Select your favorite fruit:
    <select id="mySelect">
      <option value="apple" selected >Select fruit</option>
      <option value="apple">Apple</option>
      <option value="orange">Orange</option>
      <option value="pineapple">Pineapple</option>
      <option value="banana">Banana</option>
    </select>
    </form>
    </body>
    </html>

Django code:


Inside views.py


def view(request):

    if request.method == 'POST':
        print request.body
        data = request.body
        return HttpResponse(json.dumps(data))

-2
投票

使用Angular,您应该添加标头以请求或将其添加到模块配置标头:{'Content-Type': 'application/x-www-form-urlencoded'}

$http({
    url: url,
    method: method,
    timeout: timeout,
    data: data,
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})

-4
投票

request.POST只是一个类似字典的对象,所以只需用dict语法索引它。

假设您的表单字段是fred,您可以执行以下操作:

if 'fred' in request.POST:
    mydata = request.POST['fred']

或者,使用表单对象来处理POST数据。


60
投票

我有同样的问题。我一直在发布一个复杂的JSON响应,我无法使用request.POST字典读取我的数据。

我的JSON POST数据是:

//JavaScript code:
//Requires json2.js and jQuery.
var response = {data:[{"a":1, "b":2},{"a":2, "b":2}]}
json_response = JSON.stringify(response); // proper serialization method, read 
                                          // http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/
$.post('url',json_response);

在这种情况下,您需要使用aurealus提供的方法。读取request.body并使用json stdlib反序列化它。

#Django code:
import json
def save_data(request):
  if request.method == 'POST':
    json_data = json.loads(request.body) # request.raw_post_data w/ Django < 1.4
    try:
      data = json_data['data']
    except KeyError:
      HttpResponseServerError("Malformed data!")
    HttpResponse("Got json data")

32
投票

方法1

客户:发送为JSON

$.ajax({
    url: 'example.com/ajax/',
    type: 'POST',
    contentType: 'application/json; charset=utf-8',
    processData: false,
    data: JSON.stringify({'name':'John', 'age': 42}),
    ...
});

//Sent as a JSON object {'name':'John', 'age': 42}

服务器:

data = json.loads(request.body) # {'name':'John', 'age': 42}

方法2

客户:发送为x-www-form-urlencoded (注意:contentTypeprocessData已经改变,不需要JSON.stringify

$.ajax({
    url: 'example.com/ajax/',
    type: 'POST',    
    data: {'name':'John', 'age': 42},
    contentType: 'application/x-www-form-urlencoded; charset=utf-8',  //Default
    processData: true,       
});

//Sent as a query string name=John&age=42

服务器:

data = request.POST # will be <QueryDict: {u'name':u'John', u'age': 42}>

改变了1.5+:https://docs.djangoproject.com/en/dev/releases/1.5/#non-form-data-in-http-requests

HTTP请求中的非表单数据: request.POST将不再包含通过标头中具有非特定于表单的内容类型的HTTP请求发布的数据。在以前的版本中,使用除multipart / form-data或application / x-www-form-urlencoded之外的内容类型发布的数据仍将最终在request.POST属性中表示。希望访问这些案例的原始POST数据的开发人员应该使用request.body属性。

大概相关


23
投票

request.raw_response现已弃用。使用request.body来处理非传统的表单数据,例如XML有效负载,二进制映像等。

Django documentation on the issue


16
投票

重要的是要记住Python 3有一种不同的表示字符串的方式 - 它们是字节数组。

使用Django 1.9和Python 2.7并在主体(而不是标题)中发送JSON数据,您将使用以下内容:

mydata = json.loads(request.body)

但是对于Django 1.9和Python 3.4,你会使用:

mydata = json.loads(request.body.decode("utf-8"))

我刚刚完成了这个学习曲线,制作了我的第一个Py3 Django应用程序!


9
投票

在django 1.6 python 3.3上

客户

$.ajax({
    url: '/urll/',
    type: 'POST',
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify(json_object),
    dataType: 'json',
    success: function(result) {
        alert(result.Result);
    }
});

服务器

def urll(request):

if request.is_ajax():
    if request.method == 'POST':
        print ('Raw Data:', request.body) 

        print ('type(request.body):', type(request.body)) # this type is bytes

        print(json.loads(request.body.decode("utf-8")))

5
投票

HTTP POST有效负载只是一堆扁平的字节。 Django(与大多数框架一样)根据URL编码参数或MIME多部分编码将其解码为字典。如果您只是将JSON数据转储到POST内容中,Django将不会解码它。从完整的POST内容(不是字典)中进行JSON解码;或者将JSON数据放入MIME多部分包装器中。

简而言之,显示JavaScript代码。问题似乎就在那里。


5
投票

request.raw_post_data已被弃用。请改用request.body


3
投票

像这样的东西。它起作用:从客户端请求数据

registerData = {
{% for field in userFields%}
  {{ field.name }}: {{ field.name }},
{% endfor %}
}


var request = $.ajax({
   url: "{% url 'MainApp:rq-create-account-json' %}",
   method: "POST",
   async: false,
   contentType: "application/json; charset=utf-8",
   data: JSON.stringify(registerData),
   dataType: "json"
});

request.done(function (msg) {
   [alert(msg);]
   alert(msg.name);
});

request.fail(function (jqXHR, status) {
  alert(status);
});

处理服务器上的请求

@csrf_exempt
def rq_create_account_json(request):
   if request.is_ajax():
       if request.method == 'POST':
           json_data = json.loads(request.body)
           print(json_data)
           return JsonResponse(json_data)
   return HttpResponse("Error")
© www.soinside.com 2019 - 2024. All rights reserved.