使用AFNetworking 2 POST请求时的Cocoa错误3840

时间:2013-11-01 04:39:10

标签: ios objective-c afnetworking

我每次调用此函数并返回Cocoa Error 3840。 我尝试调试并修复它,并在请求时出错,而不是在解析结果时出错


我在请求时在故障块中发现了这个错误。

Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x109230960 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not et.}
2013-11-01 12:09:30.925 MagicBox[87431:70b] The operation couldn’t be completed. (Cocoa error 3840.)

这是我的代码 在此先感谢。

- (void)loginWithUserName:(NSString *)userName
             Password:(NSString *)password
           orFacebook:(NSString *)facebookID
    withResponseBlock:(ResponseBlock)responseBlock {

if (!userName && !facebookID) {
    NSError *error = [NSError errorWithDomain:@"Missing Parameters"
                                         code:400
                                     userInfo:@{ NSLocalizedDescriptionKey : @"Username or FacebookID is required"}];
    responseBlock(error, nil);
}

NSDictionary *params;

if (facebookID) {
    params = @{ @"fb_id": facebookID };
} else {
    params = @{ @"username": userName,
                @"password": password };
}

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSString *loginURL = [self requestWithPath:@"/api/login"];
[manager POST:loginURL
   parameters:params
constructingBodyWithBlock:nil
      success:^(AFHTTPRequestOperation *operation, id responseObject) {
          if (responseBlock) {
              responseBlock(nil, responseObject);
          }
      }
      failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          if (responseBlock) {
              responseBlock(error, nil);
          }
      }];
}

4 个答案:

答案 0 :(得分:17)

我通过添加以下代码行解决了这个问题

  

manager.responseSerializer = [AFHTTPResponseSerializer序列化程序];

答案 1 :(得分:4)

一直困扰着同样的问题。

我解决了这个问题:

    self.requestSerializer = [AFJSONRequestSerializer serializer];

即。仔细检查AFHTTPRequestOperationManager是否使用了正确的请求序列化程序。

希望这可以帮到你!

答案 2 :(得分:3)

我通过google电子表格XML响应(而不是JSON)确保返回的数据处于正确的序列化中来解决这个问题

manager.responseSerializer = [AFXMLParserResponseSerializer serializer];

答案 3 :(得分:2)

我通过添加以下代码行解决了这个问题

manager.requestSerializer = [AFJSONRequestSerializer序列化程序];