Alamofire额外参数'方法'呼叫

时间:2017-07-30 05:50:59

标签: swift3 alamofire

let params = "name=Thobio Joseph&googleid=24356567890uyy4546&email=jthobio2@gmail.com&avatar=https://media.licdn.com/mpr/mpr/shrinknp_200_200/AAEAAQAAAAAAAAv0AAAAJDZjZGJjMTFjLWNiNzAtNGYzNy1iMDE4LTA2MzBmNzUwZGExNQ.jpg"

func postMethodUploadDataToServerLoginPage() {

Alamofire.request(loginUrl, method:.post,parameters:params.data(using: String.Encoding.utf8),encoding:URLEncoding.default).responseJSON { response in

switch response.result {

case .success:
                print(response)

case .failure(let error):
                failure(0,"Error")

}

}

1 个答案:

答案 0 :(得分:0)

首先你的params是错的,你需要传递一个字典[key:value],然后你需要将它转换为json并放入你的请求体中,如果你使用的是Alamofire.request你不需要传递urlString,只有一个请求,尝试使用此代码

func postMethodUploadDataToServerLoginPage() {

let paramToSend = ["name":"Thobio Joseph","googleid":"24356567890uyy4546","email":"jthobio2@gmail.com","avatar":"https://media.licdn.com/mpr/mpr/shrinknp_200_200/AAEAAQAAAAAAAAv0AAAAJDZjZGJjMTFjLWNiNzAtNGYzNy1iMDE4LTA2MzBmNzUwZGExNQ.jpg"]

let request = NSMutableURLRequest(url: URL(string: loginUrl)!)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = try! JSONSerialization.data(withJSONObject: parameters)

Alamofire.request(request).responseJSON { (response) in

switch response.result {

case .success:
                print(response)

case .failure(let error):
                failure(0,"Error")

}

}

希望这有帮助