I\'m using AFNetworking 2.0 to read JSON from a service I\'m building (on localhost
for now) in Node. Pretty normal stuff.
Node is sending JSON like so
It seems that the server is sending "text/html", and this type is not supported by default. Add @"text/html" for "acceptableContentTypes"
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
Updated Solution for Swift.
If you are using AFNetworking in Swift. Then this solution might help you. It will accept most of the content types.
let manager=AFHTTPRequestOperationManager()
manager.responseSerializer = AFJSONResponseSerializer(readingOptions: NSJSONReadingOptions.AllowFragments) as AFJSONResponseSerializer
manager.requestSerializer = AFJSONRequestSerializer() as AFJSONRequestSerializer
manager.responseSerializer.acceptableContentTypes = NSSet(objects:"application/json", "text/html", "text/plain", "text/json", "text/javascript", "audio/wav") as Set<NSObject>
Just use:
SWIFT:
manager.responseSerializer.acceptableContentTypes = NSSet(object: "text/plain") as Set<NSObject>
OBJ-C:
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
Work for me.
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
I have followed the answer posted by @KIO but it did not work in my scenario.
After going with many answers i was able to find solution which worked for me.
AFHTTPRequestOperationManager *operation = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:url];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
AFJSONResponseSerializer *jsonResponseSerializer = [AFJSONResponseSerializer serializer];
NSMutableSet *jsonAcceptableContentTypes = [NSMutableSet setWithSet:jsonResponseSerializer.acceptableContentTypes];
[jsonAcceptableContentTypes addObject:@"text/plain"];
jsonResponseSerializer.acceptableContentTypes = jsonAcceptableContentTypes;
operation.responseSerializer = jsonResponseSerializer;
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
work for me.