我正在尝试使用 Django 向服务器发出简单的 POST 请求。我可以使用另一个 api 客户端进行 post 调用只是为了对其进行测试,但是在使用 jquery 时它永远不会返回任何内容或显示来自回调函数的警报。下面是我的 jquery 和 django 代码。我已经替换了 api 调用,但我知道它是正确的。
$(document).ready(function(){
$("#signInSubmit").click(function(){
alert("Posting email: "+$("#email").val()+" guests: "+$("#guests").val());
$.post("apicall",
{
'email':$("#email").val(),
'guests':$("#guests").val(),
'event':"1"
},
function(data,status) {
alert("Data: " + data + "\nStatus: " + status);
});
});
});
这是被击中的 django 视图:
@csrf_exempt
def participant_info(request):
if request.method == 'GET':
participant_email = request.GET.get('email')
participant = Participant.objects.get(email = participant_email)
#serialized_obj = serializers.serialize('json', [ participant, ])
response = HttpResponse()
response.content = serialized_obj = serializers.serialize('json', [ participant, ])
response['Content-Type'] = 'application/json'
return response
#return HttpResponse(response, mimetype="application/json")
if request.method == 'POST':
participant_email = request.POST.get('email', '')
numguests = request.POST.get('guests', '')
eventid = request.POST.get('event', '')
participantkey = Participant.objects.get(email = participant_email)
eventkey = Event.objects.get(id=eventid)
per = Participant_Event_Record(guests = numguests, event = eventkey, participant = participantkey)
per.save()
response = HttpResponse()
response.content = serialized_obj = serializers.serialize('json', [ per, ])
response['Content-Type'] = 'application/json'
return response