最近遇到的坑,兩個(gè)后臺(tái)公般,一個(gè)表單万搔,一個(gè)Json格式,之前一直沒(méi)搞明白
附上源碼供參考
// 1.創(chuàng)建一個(gè)請(qǐng)求管理者
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//添加多的請(qǐng)求格式
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"application/x-www-form-urlencoded",@"text/plain",@"text/json", @"text/javascript",@"text/html",nil];
if (![URL containsString:@"##"]) {
//申明請(qǐng)求的數(shù)據(jù)是json類型官帘,兩種寫(xiě)法
manager.requestSerializer=[AFJSONRequestSerializer serializer];
// [manager.requestSerializer setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
}
else
{
//默認(rèn)是表單
// [manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
}
URL = [URL componentsSeparatedByString:@"##"][0];
NSLog(@"Url-->%@,\nParam-->%@",URL,parameters);
[manager POST:URL parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
// progress(uploadProgress);
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
[SVProgressHUD dismiss];
if (success) {
success(responseObject);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[SVProgressHUD dismiss];
if (failure) {
failure(error);
}
}];