AFNetworking Post 出错 Code=-1011 Request failed: bad request (400)

释放双眼,带上耳机,听听看~!

今天使用新版AFNetWorking(3.0 beta3) 竟然出错了,错误信息如下:

Error Domain=com.alamofire.error.serialization.response Code=-1011 “Request failed: bad request (400)” UserInfo=0x7fbf4947edf0
{com.alamofire.serialization.response.error.response=…

遇到这种错误不要慌,先看看你post时带的参数params是不是有问题,是服务器不认识的格式?或者是…你字段写错了!仔细检查下你的字段是不是写错了…

Stack Overflow上有位大神这样说:

The
error says it all: you got a 400
response
 from the server, meaning that what you sent was either not formatted properly, or the server just couldn’t understand it.

还不行的话,请与你们后台联系..

你可以这样查看返回的中文错误信息:

NSData *data = error.userInfo[@"com.alamofire.serialization.response.error.data"] ;
NSString *errorStr = [[ NSString alloc ] initWithData:data encoding:NSUTF8StringEncoding];

 

/**
*  根据code获得一个accessToken(发送一个POST请求)
*
*  @param code 授权成功后的请求标记
*/
- (void)accessTokenWithCode:(NSString *)code
{
// 1.获得请求管理者
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
// 2.封装请求参数
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"client_id"] = JXAppKey;
params[@"client_secret"] = JXAppSecret;
params[@"redirect_uri"] = JXRedirectURI;
params[@"grant_type"] = @"authorization_code";
params[@"code"] = code;
// 3.发送post请求
[mgr POST:@"https://api.weibo.com/oauth2/access_token" parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *accountDict) {
// 隐藏HUD
[MBProgressHUD hideHUD];
JXLog(@"请求成功--%@", accountDict);
// 字典转成模型
JXAccount *account = [JXAccount accountWithDict:accountDict];
// 存储账号模型
[JXAccountTool saveAccount:account];
// 切换控制器(可能去新特性tabbar)
[[UIApplication sharedApplication].keyWindow switchRootViewController];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
// 隐藏HUD
[MBProgressHUD hideHUD];
JXLog(@"请求失败--%@", error);
}];

 

人已赞赏
iOS文章

Auto property synthesis will not synthesizeproterty;it will be implemented by its superclass, use@dy

2020-5-20 5:31:54

iOS文章

iOS教你改变UITextFiled的placeholder颜色

2020-5-20 6:46:58

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索