Alamofire를 사용하여 서버에 요청하고 있습니다. 내가하는 방법은 다음과 같습니다.
Alamofire.request(url, method: .post, parameters: [:] ,encoding: JSONEncoding.default).responseJSON { response in
print("response=\(response)")
print("Response=:\((response.response?.statusCode)!)")
switch response.result{
case .success :
let passList = AuthenticateSuccess(nibName: "AuthenticateSuccess", bundle: nil)
self.navigationController?.pushViewController(passList, animated: true)
print("connected")
case .failure(let error):
self.showAlertTost("", msg: "Authentication Failed. Authenticate again!", Controller: self)
}
}
다음과 같이 인쇄됩니다.
response=SUCCESS: {
message = "Access denied.";
}
Response=:401
connected
401이 오류이면 왜 성공 블록이 실행되고 있는지 알고 싶습니다. Alamofire의 실패 사례는 다르게 처리됩니까?
로 설명서를 말한다 :
기본적으로 Alamofire는 응답 내용에 관계없이 완료된 요청을 성공으로 처리합니다.
validate()
응답 처리기 전에 호출 하면 응답에 허용되지 않는 상태 코드 또는 MIME 유형이있는 경우 오류가 생성됩니다.
예
Alamofire.request(url, method: .post, encoding: JSONEncoding.default)
.validate()
.responseJSON { response in
...
}
를 사용하면 validate
2xx가 아닌 응답이 이제 오류로 처리됩니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다