AFNetworking - AFHTTPRequestOperationManager不返回数据

时间:2015-01-16 07:44:03

标签: ios iphone afnetworking

我正在使用AFHTTPRequestOperationManager进行登录。

- (IBAction)loginAction
{
    [TGProjectHandler clearCookies];

    NSDictionary *params = @{@"Email": _email.text,
                             @"Password": _password.text,
                             @"IsRemember": @"true",
                             @"ReturnUrl": @"",
                             @"UserId": @0,
                             @"OutResponse": @0};

    [_sharedHandler.requestManager POST:TGURL_LOGIN
                             parameters:params
     success:^(AFHTTPRequestOperation *operation, id responseObject) {

     NSError *e;
     NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:[operation.responseString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&e];                                    
      NSLog(@"%@\n\n%@", jsonDict, responseObject);
      NSString *outResponse = responseObject[@"Object"][@"OutResponse"];
      if (outResponse.integerValue == 1){
         NSLog(@"login successful: %@", outResponse);
         [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"User_Logged_In"];
         [TGProjectHandler saveCookiesToDefaults];
         [self performSegueWithIdentifier:@"HomePage" sender:self];
       }else
        {
           [[[UIAlertView alloc] initWithTitle:@"Login Failed" message:@"Invalid credentials" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
        }
     }
      failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         [TGProjectHandler dismissHUD];
         [[[UIAlertView alloc] initWithTitle:@"Login Failed" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
       }];
}

服务函数返回UserID,但这是我在NSLog时收到的响应。

NSLog(@"%@\n\n%@", jsonDict, responseObject); 



Object =     {
        OutResponse = 1;
        ReturnUrl = "<null>";
    };
    Success = 1;
}

{
    Object =     {
        OutResponse = 1;
        ReturnUrl = "<null>";
    };
    Success = 1;
}  

为什么 UserId 没有响应?

1 个答案:

答案 0 :(得分:1)

通过查看您的代码,我猜问题可能是您的请求中的内容类型。检查您的内容类型是否设置正确。

例如 -

[_sharedHandler.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

此外,您可以将响应序列化器更改为AFJSONResponseSerializer,它会自动将您的响应转换为字典或数组。

_sharedHandler.responseSerializer = [AFJSONResponseSerializer serializer];