本文介绍了Alamofire 2.0和Swift 2-标头不起作用。看看如何解决的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 Alamofire 2 标头 swift 2 c $ c>停止工作,没有任何代码错误。原因是标头不能使用旧样式。

When I upgraded my project to swift 2 with Alamofire 2 headers stopped working without any errors in code. The reason is that headers are not working old style.

 // 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")
                    }
            }
        }

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

You can see the fixed version below

推荐答案

您可以通过发送 headers header问题 >在请求中。

You can fix the header problem by sending headers in requests.

// 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")
            }
    }
}

这篇关于Alamofire 2.0和Swift 2-标头不起作用。看看如何解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 23:45