我使用alamofire有一段时间了,但我从来没有使用的表单数据后。现在我卡住了。我有2个PARAMS(电子邮件,密码),不知道他们怎么POST到服务器。谁能给我一个例子吗?
这里是在雨燕3.0 Alamofire 4.0示例代码
let url = "http://testurl.com"
let parameters = [
"email": "[email protected]",
"password": "55555"
]
Alamofire.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default).responseJSON { response in
switch response.result {
case .success:
if let value = response.result.value {
print(value)
}
case .failure(let error):
print(error)
}
}
所以我的解决办法是....你必须指定Alamofire的参数编码。因此,代码如下所示。
雨燕2.0
func registerNewUserFormData(completionHandler: (Bool, String?) -> ()){
// build parameters
let parameters = ["email": "[email protected]", "password": "123456"]
// build request
Alamofire.request(.POST, urlDomain + "register", parameters: parameters, encoding: .URL).responseJSON { response in
switch response.result {
case .Success:
print("Validation Successful")
if let JSON = response.result.value {
print(JSON)
}
case .Failure(let error):
print(error)
}
}
}
斯威夫特5
let url = "http://testurl.com"
let parameters = [
"email": "[email protected]",
"password": "55555"
]
AF.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default).responseJSON { response in
switch response.result {
case .success:
if let value = response.result.value {
print(value)
}
case .failure(let error):
print(error)
}
}