AFNetworking 2.0 - “unacceptable content-type: text/plain”

前端 未结 7 1762
深忆病人
深忆病人 2020-12-28 15:12

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

相关标签:
7条回答
  • 2020-12-28 15:40

    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"];
    
    0 讨论(0)
  • 2020-12-28 15:42

    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>
    
    0 讨论(0)
  • 2020-12-28 15:52

    Just use:

    SWIFT:

    manager.responseSerializer.acceptableContentTypes = NSSet(object: "text/plain") as Set<NSObject>
    

    OBJ-C:

    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    
    0 讨论(0)
  • 2020-12-28 15:58

    Work for me.

    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    
    0 讨论(0)
  • 2020-12-28 16:00

    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;
    
    0 讨论(0)
  • 2020-12-28 16:01

    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

    work for me.

    0 讨论(0)
提交回复
热议问题