Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option t

AFHTTPSessionManager *manager =[AFHTTPSessionManager manager]; [manager GET:@"http://www.xxx.com" parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) { NSString *result = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding]; NSLog(@"%@",result); } failure:^(NSURLSessionDataTask *task, NSError *error) { NSLog(@"%@",error); }];


这样的一个请求,会出现如下错误:

Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}


解决的办法:

在默认情况下,afnetworking网络请求都是以json的数据格式返回数据,因此,客户端也会以json为默认格式,如果返回的数据为非json标准格式,则会出现上述错误。

只需要在发送请求前加入:manager.responseSerializer = [AFHTTPResponseSerializer serializer]

便可以解决这个问题了。




你可能感兴趣的:(Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option t)