问题:前端传POST数据 "{\n\t'username': 'Lucky',\n\t'password': '123456'\n}"
既有\n等转义符,又有单引号。使用json解析这样的字符串时,就会报错。
解决:使用Demjson来解析
import demjson
@csrf_exempt
def login(request):
if request.method=='GET':
return render(request,'post.html')
else:
body = demjson.decode(request.body)
return JsonResponse(body)