如何使用404值添加addAcceptableStatusCodes来请求?

时间:2014-05-23 14:21:24

标签: objective-c afnetworking-2

我需要接受404值,因为API会返回一些JSON警报,解释为什么它会给出404错误请求。对于这个问题,我需要在下面添加以下行以便允许它,但是我仍然不知道自从它在POST上声明后操作值的来源。

代码行:

[operation addAcceptableStatusCodes:[NSIndexSet indexSetWithIndex:404]];

我的源代码

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *parameters = @{@"nick": @"eddwinpaz",@"pass_field":@"eeddwinpaz"};


    [manager POST:@"http://domain.com:8000/user-login/" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         NSLog(@"JSON: %@", responseObject);

     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

         NSLog(@"Error: %@", error);

         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Login Failed"
                                                         message:@"E-mail or password are wrong, Please Try Again"
                                                        delegate:nil
                                               cancelButtonTitle:@"OK"
                                               otherButtonTitles:nil];
         [alert show];
         [hud hide:YES];

     }];

1 个答案:

答案 0 :(得分:1)

将AFNetworking1.0中的一些遗留代码更新为2.0时,我遇到了类似的问题。我发现复制现有代码然后添加新代码似乎都有效。

NSMutableIndexSet *acceptedCodes = [[NSMutableIndexSet alloc] 
    initWithIndexSet:operation.responseSerializer.acceptableStatusCodes];
[acceptedCodes addIndex:304];
operation.responseSerializer.acceptableStatusCodes = [acceptedCodes copy];