AFJSONRequestOperation在200响应.statusCode上击中失败的块

时间:2013-09-11 00:36:40

标签: ios objective-c afnetworking afjsonrequestoperation

我的AFJSONRequestOperation正在响应200响应的失败阻止。这是因为我有额外的JSON吗?

 [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *d = (NSDictionary *)responseObject;
    bool required = [d[@"payment_required"] boolValue];
    [self.delegate paymentRequired:required];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    int statusCode = operation.response.statusCode;
    NSLog(@"status code: %d response: %@", statusCode, operation.responseString);
    if (operation.response.statusCode == 402) {
        [self.delegate paymentRequired:true];
        return ;
    }
    [self handleOperationFailed:operation action:^{
        [self determinePaymentRequired];
    }];
}];

在控制台中产生

status code: 200 response: {'payment_required':'false'}

为什么会这样?

2 个答案:

答案 0 :(得分:1)

根据我的经验,您需要遇到3个条件才能成功阻止:

  1. 200 响应代码
  2. 响应中包含的 JSON对象
  3. 响应的Content-Type设置为 application / json
  4. 希望这可以帮到你。

答案 1 :(得分:0)

您的JSON无效。它有单引号({'payment_required':'false'}),但JSON格式需要它们是双引号:

{
     "payment_required" : false 
}

PS:我还删除了false上的引号,因为false是有效值(并且是首选)。