使用脚本向我的Django后端发出简单的帖子请求。我希望数据采用Json格式。
<input id ="my" type="submit" onclick="me()"/>
<script>
function me()
{
var data2 =JSON.stringify ({
"crave": "romana",
"uid": "100",
"access_token": "AAA"
});
alert(data2);
$.ajax({
url: "http://localhost:8000/trial/",
type: 'POST',
contentType: "application/x-www-form-urlencoded",
data: data2,
dataType: 'json',
processData: false
});
}
</script>
在我打印request.POST的视图中,它显示以下内容
{u'{"crave":"romana","uid":"100","access_token":"AAA"}': [u'']}
enter code here
我做错了什么?
答案 0 :(得分:2)
尝试以下方法:
$.ajax({
type: 'POST',
url: 'http://localhost:8000/trial/',
data: data2, // without stringifying
success: function(res) { }
});
或作为捷径:
data2 = {crave: "romana", uid: "100", access_token: "AAA"}
$.post('http://localhost:8000/trial/', data2, function(res) { });