问题描述
我试图处理传入的JSON /使用Django / Python的Ajax请求。
I'm trying to process incoming JSON/Ajax requests with Django/Python.
request.is_ajax()
是真
的要求,但我不知道在哪里的有效载荷是JSON数据。
request.is_ajax()
is True
on the request, but I have no idea where the payload is with the JSON data.
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']
有请求键后显然没有钥匙。
There are apparently no keys in the request post keys.
当我在看帖子中萤火虫,有JSON数据被发送了请求。
When I look at the POST in Firebug, there is JSON data being sent up in the request.
推荐答案
如果你要发送的JSON到Django的,我想你想 request.body
( request.raw_post_data code>上的Django< 1.4)。这会给你通过邮寄方式发送的原始JSON数据。从那里,你可以进一步处理它。
If you are posting JSON to Django, I think you want request.body
(request.raw_post_data
on Django < 1.4). This will give you the raw JSON data sent via the post. From there you can process it further.
下面是使用JavaScript, jQuery的,jQuery的,JSON和Django的。
Here is an example using JavaScript, jQuery, jquery-json and Django.
JavaScript的:
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的:
Django:
def save_events_json(request):
if request.is_ajax():
if request.method == 'POST':
print 'Raw Data: "%s"' % request.body
return HttpResponse("OK")
Django的&LT; 1.4:
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")
这篇关于凡在我传入的Django的请求我的JSON数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!