<p><strong>方法1</strong></p>
<p>客户端:作为<code>JSON</code>发送</p>
<pre><code>$.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}
</code></pre>
<p>服务器:</p>
<pre><code>data = json.loads(request.body) # {'name':'John', 'age': 42}
</code></pre>
<p><strong>方法2</strong></p>
<p>客户端:发送为<code>x-www-form-urlencoded</code><br/>
(注:<code>contentType</code>&;<code>processData</code>已更改,不需要<code>JSON.stringify</code>)</p>
<pre><code>$.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
</code></pre>
<p>服务器:</p>
<pre><code>data = request.POST # will be <QueryDict: {u'name':u'John', u'age': 42}>
</code></pre>
<hr/>
<p>在1.5+:<a href="https://docs.djangoproject.com/en/dev/releases/1.5/#non-form-data-in-http-requests" rel="noreferrer">https://docs.djangoproject.com/en/dev/releases/1.5/#non-form-data-in-http-requests</a>中更改</p>
<blockquote>
<p><strong>Non-form data in HTTP requests</strong>
: <br/>request.POST will no longer include data posted via HTTP requests with
non form-specific content-types in the header. In prior versions, data
posted with content-types other than multipart/form-data or
application/x-www-form-urlencoded would still end up represented in
the request.POST attribute. Developers wishing to access the raw POST
data for these cases, should use the request.body attribute instead.</p>
</blockquote>
<p>可能是相关的</p>
<ul>
<li><a href="https://groups.google.com/forum/#!msg/django-developers/s8OZ9yNh-8c/yWeY138TpFEJ" rel="noreferrer">https://groups.google.com/forum/#!msg/django-developers/s8OZ9yNh-8c/yWeY138TpFEJ</a></li>
<li><a href="https://code.djangoproject.com/ticket/17942" rel="noreferrer">https://code.djangoproject.com/ticket/17942</a>。固定在1.7中</li>
<li><a href="http://homakov.blogspot.in/2012/06/x-www-form-urlencoded-vs-json-pros-and.html" rel="noreferrer">http://homakov.blogspot.in/2012/06/x-www-form-urlencoded-vs-json-pros-and.html</a></li>
</ul>