当我尝试向Web API发送JSON字符串'parametersString'时,parameterEncoderFailed出现错误消息,有人可以帮助我解决问题
失败(Alamofire.AFError.parameterEncoderFailed(原因: Alamofire.AFError.ParameterEncoderFailureReason.encoderFailed(错误: Alamofire.URLEncodedFormEncoder.Error.invalidRootObject(“ string(\” {\ n \\“ authenticate \\”: {\ n \\“ userName \\”:\\“ fan.doe \\”,\ n \\“ password \\”: \\“ 12345 \\”,\ n \“到期\\”:\\“ 2019-05-03 18:00:00 \\“ \ n} \ n} \”)“)))))))
static func Login(email:String, password:String, completion: @escaping (_ success: Bool, _ response: DataResponse<Data?>?) -> ()) {
let urlString:
let parametersString = """
{
"authenticate": {
"userName": "\(email)",
"password": "\(password)",
"expiry": "2019-05-03 18:00:00"
}
}
"""
var response:DataResponse<Data?>?
print(urlString)
AF.request(urlString, method: .post, parameters: parametersString, encoding: String.Encoding.utf8.rawValue, headers: headers )
.response { resp in
response = resp
print(response)
if(response?.response?.statusCode == 200){
completion(true, response)
}
else {
completion(false, nil)
}
}
}
}
答案 0 :(得分:1)
您可能需要发送字典[String:Any]
let parametersDic:[String:Any] = [
"authenticate": [
"userName": "\(email)",
"password": "\(password)",
"expiry": "2019-05-03 18:00:00"
]]