Alamofire 2.0和Swift 2 – Header不起作用。 看看如何解决它

当我升级我的项目Alamofire 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") } } } 

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

您可以通过在requests发送headers header problem解决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") } } }