我每次都调用这个函数并返回 Cocoa 错误 3840。 我尝试调试并修复它,它在请求时出错,而不是在解析结果时出错
我在请求时在失败 block 中发现了这个错误。
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);
}
}];
}
最佳答案
我通过添加以下代码行解决了这个问题
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
关于ios - 使用 AFNetworking 2 进行 POST 请求时 cocoa 错误 3840,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19720890/