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