今天项目在使用AFN3.1时,执行如下代码出现错Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed
NSString *account = self.userAccountTextField.text;
NSString *password = self.passwordTextField.text;
// 1.1.设置请求参数
NSString *postUrlStr = [QYBaseUrl stringByAppendingString:@"userLogin"];
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"username"] = account ; // @"18513138246"
params[@"password"] = password; // @"test111"-->md5;
// 2.发送请求
[SVProgressHUD show];
[self.manager POST:postUrlStr parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
QYLog(@"响应------%@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
QYLog(@"🐶----错误---%@",error);
[SVProgressHUD showInfoWithStatus:@"网络异常\n请检查网络,稍后再试"];
}];
解决办法是: 在发送请求前加上如下代码,为sessionManager实例设置acceptableContentTypes
self.manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];