From the client side, I receive some data by ajax post. The data type is json format.
function sendProjectBaseInfo() {
prj = {
id : $('#id').val(),
email : $('#email').val(),
title : $('#title').val(),
}
$.ajax({
url: '/prj/',
type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: prj,
success: function(result) {
alert(result.Result)
}
});
}
After got the json data, I try to convert to json, or dict format. To convert to json. I wrote like this:
import json
def post(self, request):
if request.is_ajax():
if request.method == 'POST':
json_data = json.loads(request.data)
print('Raw Data : %s'%request.body)
return HttpResponse('OK!')
in case of above, I got 500 Internal server Error.
So I wrote like below to work around this error.
import json
def post(self, request):
if request.is_ajax():
if request.method == 'POST':
data = json.dumps(request.data)
print('Raw Data : %s'%request.body)
return HttpResponse('OK!')
After all I got the same error. So I was look into the requested data.
import json
def post(self, request):
if request.is_ajax():
if request.method == 'POST':
print('Raw Data : %s'%request.body)
return HttpResponse('OK!')
Print out is :
Raw Data : b'{"id":"1","email":"[email protected]","title":"TEST"}'
How can I overcome this situation?