【问题标题】:Alamofire 2.0 and Swift 2 - Header is not working. See how to fix itAlamofire 2.0 和 Swift 2 - 标头不工作。看看如何修复它
【发布时间】:2015-09-18 09:00:47
【问题描述】:

当我使用Alamofire 2 将我的项目升级到swift 2 时,headers 在代码中没有任何错误的情况下停止工作。原因是headers 不是老式的。

 // login with Alamofire 1 and Swift 1.2 - WITH HEADER
 func loginAlamofire_1(username:String) {
            manager.session.configuration.HTTPAdditionalHeaders = ["Authorization": "yourToken"]
            manager.request(.POST, "login_url", parameters: ["username" : username], encoding: ParameterEncoding.JSON)
                .response{ (request, response, data, error) -> Void in
                    if error != nil{
                        print("error!")
                    } else {
                        print("welcome")
                    }
            }
        }

你可以在下面看到固定版本

【问题讨论】:

    标签: ios swift swift2 alamofire


    【解决方案1】:

    您可以通过在requests 中发送headers 来修复header problem

    // login with Alamofire 2.0 and Swift 2.0 - WITH HEADER
    func loginAlamofire_2(username:String) {
        manager.request(.POST, "login_url", parameters: ["username" : username], encoding: ParameterEncoding.JSON, headers: ["Authorization": "yourToken"])
            .response{ (request, response, data, error) -> Void in
                if error != nil{
                    print("error!")
                } else {
                    print("welcome")
                }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-08
      • 2019-08-06
      • 2019-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多