我写了一个django webservice,假设我的iOS应用程序正在使用它。 我已经设法让GET请求工作没有特别的问题,但是当我开始使用POST时,我得到403。 我读到它可能是csrf所以我试过
from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
当然有效!
这是我的django视图代码
questionAnswer = request.POST['questionAnswer']
facebookID = request.POST['facebookID']
if Question.objects.get(pk=int(question_id)).meaning == questionAnswer:
u = User.objects.get(fbid = facebookID)
u.questions_answered = u.questions_answered + 1
u.save()
dictForSend = {'didAnswerCorrect' : 'True'}
jsonq = json.dumps(dictForSend)
return HttpResponse(jsonq, content_type='application/json')
else:
dictForSend = {'didAnswerCorrect' : 'False'}
jsonq = json.dumps(dictForSend)
return HttpResponse(jsonq, content_type='application/json')
这是我的目标c代码
NSDictionary *postDict = @{ @"questionAnswer": questionAnswer, @"facebookID": facebookID};
NSString *myDictInJSON = [postDict JSONRepresentation];
NSData *postData = [myDictInJSON dataUsingEncoding:NSUTF8StringEncoding];
NSString *URLForSend = [NSString stringWithFormat:(@"http://127.0.0.1:8000/questions/checkanswer/%@/") , questionID ];
NSURL *url = [NSURL URLWithString:URLForSend];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"%@", JSON);
} failure:nil];
[operation start];
终端线
"POST /questions/checkanswer/1/ HTTP/1.1" 403 2282
知道如何在我的应用中加入csrf吗?