1 回答
TA贡献1966条经验 获得超4个赞
发送数据时出现问题。
当我用来发布数据并接受视图时,一切正常JSON.stringifyjson.loads(request.POST.get('frm'))
$('#messageform').on('submit', function(e) {
e.preventDefault();
var frm = {
'user_id': $('#user_id').attr('value'),
'text': $('#form_message').val()
};
frm = JSON.stringify(frm);
$.ajax({
type: 'POST',
url: "message/message_form/",
dataType: 'json',
data: {
csrfmiddlewaretoken: "{{ csrf_token }}",
'frm': frm
},
})
});
views.py
def post(self, request):
dictionary = json.loads(request.POST.get('frm'))
print('=================',dictionary)
u = get_object_or_404(User,pk=dictionary['user_id'])
msg = message.objects.filter(sender=request.user).filter(receiver=u).all() | message.objects.filter(sender=u).filter(receiver=request.user).all()
u = message.objects.create(sender=request.user,receiver=u,text=dictionary['text'])
#if request.user != u and u.objects.filter(receiver__sender=request.user,receiver__receiver=u,receiver__read=False).exists :
# notify.send(request.user, recipient=u, verb="message you please read in message box",public=False)
# msg = message.objects.filter(sender=request.user).filter(receiver=u).all() | message.objects.filter(sender=u).filter(receiver=request.user).all()
msg = dictionary['text']
data = { "message":msg}
return HttpResponse(json.dumps(data), content_type='application/json')
添加回答
举报