我正在尝试使用swift从我的iOS应用程序进行API GET调用,我得到的错误(我在本地服务器上进行测试)如下:
[07 / Sep / 2015 18:04:26]“GET / user_management / api / v1 / user / HTTP / 1.1”401 0
以下是代码:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
var url : String = "http://127.0.0.1:8000/user_management/api/v1/user/"
var request : NSMutableURLRequest = NSMutableURLRequest()
request.URL = NSURL(string: url)
request.HTTPMethod = "GET"
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler:{ (response:NSURLResponse!, data: NSData!, error: NSError!) -> Void in
var error: AutoreleasingUnsafeMutablePointer<NSError?> = nil
let jsonResult: NSArray! = NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.MutableContainers, error: error) as? NSArray
if (jsonResult != nil) {
// process jsonResult
println("success")
} else {
// couldn't load JSON, look at error
println("fail")
}
})
return true
}
控制台不断打印“失败”。
答案 0 :(得分:0)
而不仅仅println("fail")
至少做println("\(error.localizedDescription())")
它告诉你的是,呼叫未被授权(这就是HTTP代码401的含义,只要你通过HTTP代码获得失败的结果,就试着用谷歌搜索“ http代码401 ”)您需要先了解如何传递身份验证,或正确响应身份验证质询。