我想创建Alamofire POST请求 但是我收到失败响应,我不知道映射对象或请求中的错误
func loginUser() {
let URL = ..... + "/Login"
let params2 = "{\"MobileNumber\":\"\(MobileNumber.text!)\"}"
Alamofire.request(URL, method: .post, parameters: [:], encoding: params2, headers: [
"Content-Type": "application/json"]).responseObject { (response: DataResponse<Login>) in
if(response.result.isFailure){
print ("failure")
print (response.result.description)
return
}
else{
print(response.result) // result of response serialization
self.LoginUser = response.result.value
}
}
答案 0 :(得分:0)
似乎您正在将请求方法的编码属性设置为param2
变量。
尝试将参数设置为param2
并更新编码以使用.default
:
Alamofire.request(URL, method: .post, parameters: param2, encoding: .default, headers: ...
答案 1 :(得分:0)
您没有将正确的值传递到必填字段中。请在下面使用更正的版本,希望对您有帮助
func loginUser() {
let URL = ..... + "/Login"
let params2: [String: Any] = ["MobileNumber" : "\(MobileNumber.text!)"]"
let header = ["Content-Type": "application/json"]
Alamofire.request(URL, method: .post, parameters: params2, encoding: JSONEncoding.default, headers: header).responseObject { (response: DataResponse<Login>) in
if(response.result.isFailure){
print ("failure")
print (response.result.description)
return
}
else{
print(response.result) // result of response serialization
self.LoginUser = response.result.value
}
}