我正在调用我的注册服务,其中参数正常,url也正确,我得到状态代码200作为响应,但是当我打印结果和响应时,它显示失败。我已经检查了所有注册所需的一切看起来很好,但我很困惑为什么它显示失败的回应。它显示了这一点,
["FirstName": "Awais", "Email": "awais@gmail.com", "PhoneNumber": "08965235415", "Password": "123456", "LastName": "Ahmed", "ConfirmPassword": "123456"]
Request: Optional(MyURL)
Response: Optional(<NSHTTPURLResponse: 0x604000438900> { URL: http://api.theahsanzaman.info/api/Account/Register } { Status Code: 200, Headers {
"Cache-Control" = (
"no-cache"
);
"Content-Length" = (
0
);
Date = (
"Tue, 17 Apr 2018 13:37:03 GMT"
);
Expires = (
"-1"
);
Pragma = (
"no-cache"
);
Server = (
"Microsoft-IIS/8.5"
);
"X-AspNet-Version" = (
"4.0.30319"
);
"X-Powered-By" = (
"ASP.NET"
);
"X-Powered-By-Plesk" = (
PleskWin
);
} })
Result: FAILURE
RR FAILURE: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength)
0
1111
"FAILURE: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength)"
我的请求代码就是这个,
import SwiftyJSON
import Alamofire
import UIKit
class SignUpService{
static let instance = SignUpService()
var status = 0
func signUpUser(param:[String: String],completion:@escaping CompletionHandler) {
let url = URL.init(string: SignUpUrl)
Alamofire.request(url!, method: .post, parameters: param, encoding:JSONEncoding.default, headers: nil).responseJSON { (response) in
print("Request: \(String(describing: response.request))") // original url request
print("Response: \(String(describing: response.response))") // http url response
print("Result: \(response.result)")// response serialization result
print("RR \(response)")
if response.result.error == nil{
self.status = (response.response?.statusCode)!
if(self.status == 200){
print("successful")
}
completion(true)
print("11112222")
}else{
completion(false)
print("1111")
debugPrint(response.description as Any)
}
}
}
}
我很困惑,如果状态代码为200,为什么它没有显示响应成功。
答案 0 :(得分:0)
通过在alamofire请求中添加validate(statusCode:200 ..&lt; 299)来调用API时验证响应状态代码。
如果成功代码为200,您的代码将会成功。
Alamofire.request(url!, method: .post, parameters: param, encoding:JSONEncoding.default, headers: nil). validate(statusCode: 200..<300).responseJSON { (response) in
switch response.result {
case .success(let data):
Logs.DLog(object: "\n Success: \(response)")
// Do your code here...
case .failure(let error):
Logs.DLog(object: "\n Failure: \(error.localizedDescription)")
// Do your code here...
}
})
但是你没有得到响应JSON,所以你无法解码响应JSON。