AFNetworking - AFHTTPRequestOperationManager 没有 return 数据

AFNetworking - AFHTTPRequestOperationManager does not return data

我正在使用 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];
       }];
}

服务功能 returns UserID,但这是我在 NSLog 时得到的响应。

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



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

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

为什么 UserId 没有响应?

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

例如 -

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

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

_sharedHandler.responseSerializer = [AFJSONResponseSerializer serializer];