Cocoa Error 3840 when POST request with AFNetworking 2

I call this function and return Cocoa Error 3840 every time. I try to debug and fix it, and this is a mistake in the request, and not in the analysis of the result


I found this error in request failure blocks.

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.)

What is my code Thanks in Advance.

- (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
source share
4 answers

I solved this by adding the following line of code

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

+16
source

Stuck with the same problem.

I solved it as follows:

    self.requestSerializer = [AFJSONRequestSerializer serializer];

i.e. double check if the AFHTTPRequestOperationManagercorrect query serializer is using .

Hope this helps you!

+4

I solved this by making sure that the returned data is in the correct serialization with google XML table response (instead of JSON)

manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
+3
source

I solved this by adding the following line of code

manager.requestSerializer = [AFJSONRequestSerializer serializer];

+2
source

All Articles