0、干货先写在前
1、前端传值的数据必须使用JSON.stringify()传化
2、后端,通过request.body接收数据,直接使用json.loads解析,解析前,先decode一下:receive_data = json.loads(request.body.decode())。如果使用simplejson.loads(request.body),就不用decode()
下面是这个问题产生及解决的过程,还有一些可能的应用场景。
1、传统方式解析表单数据
之前用Django后台接收数据的时候,一直采用的是表单的形式,通过Ajax传值的时候也是一样,直接通过键值对儿将值传递给后台,前端代码类似这样:
$.post("/login/",
{
"user":"threedog",
"password":"123456"
},
function (res) {
console.log(res)
});
在后台采用`request.POST`进行数据接收:
class Login(View):
def get(self,request):
return render(request,'login.html')
def post(self,request):
print(request.POST)
print("user :",end='')
print(requ