我从 iPhone 向 Django 发送 POST 请求,并收到“CSRF 验证失败”,我无法完全理解。 我试图在互联网上找到一个好的解决方案,但我找不到。 有没有简单的方法可以 POST 到 django?
这是我的代码:
NSString *post =[NSString stringWithFormat:@"s=aaa&r=k&c=gg"];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:(@"http://localhost:8000/messages/views/")]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(data);
最佳答案
我错了还是在 native 应用程序上使用它没有意义?
在这种情况下,您可以使用此装饰器禁用此保护:
from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
def view_without_csrf_protection(request):
pass
关于iPhone POSTing Django 并获取 CSRF 验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8487542/