我怎样才能让 django 停止返回 "Response code: 403" 到 iPhone App
How can i get django to stop returning "Response code: 403" to iPhone App
我的 iPhone 应用程序有以下 swift 代码:
var post:NSString = "username=\(username)&password=\(password)"
NSLog("PostData: %@",post);
var url:NSURL = NSURL(string: "http://localhost:8000/straightred/login/")!
var postData:NSData = post.dataUsingEncoding(NSASCIIStringEncoding)!
var postLength:NSString = String( postData.length )
var request:NSMutableURLRequest = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.HTTPBody = postData
request.setValue(postLength, forHTTPHeaderField: "Content-Length")
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
var reponseError: NSError?
var response: NSURLResponse?
var urlData: NSData? = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&reponseError)
let res = response as NSHTTPURLResponse!;
NSLog("Response code: %ld", res.statusCode);
上面的代码在 xcode 输出 window 中给出了以下结果:
2015-04-22 20:30:03.768 SwiftLoginScreen[2290:70315] PostData: username=test&password=password
2015-04-22 20:30:03.821 SwiftLoginScreen[2290:70315] 响应代码:403
下面是 django views.py 部分(尽管老实说,这里放什么似乎并不重要,因为在任何代码 运行 之前返回错误 403):
@require_POST
def login(request):
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user and user.is_active:
return HttpResponse("{\"succes:1\" }",content_type = 'application/json')
else:
return HttpResponse("{\"success\":0,\"error_message\":\"Invalid Data\"}",content_type = 'application/json')
因此,总而言之,我是否需要向我的 iphone 应用程序和/或 Django 设置添加一些内容以允许来自我的 iPhone 应用程序的请求?
非常感谢任何帮助,非常感谢,Alan。
听起来像是一个 CSRF protection 相关的问题。
尝试使用 csrf_exempt.
装饰 iPhone 应用访问的视图
我的 iPhone 应用程序有以下 swift 代码:
var post:NSString = "username=\(username)&password=\(password)"
NSLog("PostData: %@",post);
var url:NSURL = NSURL(string: "http://localhost:8000/straightred/login/")!
var postData:NSData = post.dataUsingEncoding(NSASCIIStringEncoding)!
var postLength:NSString = String( postData.length )
var request:NSMutableURLRequest = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.HTTPBody = postData
request.setValue(postLength, forHTTPHeaderField: "Content-Length")
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
var reponseError: NSError?
var response: NSURLResponse?
var urlData: NSData? = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&reponseError)
let res = response as NSHTTPURLResponse!;
NSLog("Response code: %ld", res.statusCode);
上面的代码在 xcode 输出 window 中给出了以下结果:
2015-04-22 20:30:03.768 SwiftLoginScreen[2290:70315] PostData: username=test&password=password
2015-04-22 20:30:03.821 SwiftLoginScreen[2290:70315] 响应代码:403
下面是 django views.py 部分(尽管老实说,这里放什么似乎并不重要,因为在任何代码 运行 之前返回错误 403):
@require_POST
def login(request):
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user and user.is_active:
return HttpResponse("{\"succes:1\" }",content_type = 'application/json')
else:
return HttpResponse("{\"success\":0,\"error_message\":\"Invalid Data\"}",content_type = 'application/json')
因此,总而言之,我是否需要向我的 iphone 应用程序和/或 Django 设置添加一些内容以允许来自我的 iPhone 应用程序的请求?
非常感谢任何帮助,非常感谢,Alan。
听起来像是一个 CSRF protection 相关的问题。
尝试使用 csrf_exempt.
装饰 iPhone 应用访问的视图