Post Request with AFHttpRequestOperationManager not working

前端 未结 2 1762
名媛妹妹
名媛妹妹 2020-12-16 15:08

I am using AFHTTPRequestOperationManager to post some JSON to my server, my code is below.

NSDictionary *parameters = [[NSDictionary alloc] initWithObjectsAn         


        
相关标签:
2条回答
  • 2020-12-16 15:34

    You need to set your request and response serializers to handle JSON using AFJSONRequestSerializer and AFJSONResponseSerializer before executing your request. Using an NSDictionary literal for your parameters helps code clarity as well:

    AFSecurityPolicy *policy = [[AFSecurityPolicy alloc] init];
    [policy setAllowInvalidCertificates:YES];
    
    AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager];
    [operationManager setSecurityPolicy:policy];
    operationManager.requestSerializer = [AFJSONRequestSerializer serializer];
    operationManager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    [operationManager POST:@"posturl here" 
                parameters: @{@"name":  @"john",
                            @"email":   @"xxxxx@gmail.com",
                            @"password: @"xxxxx",
                            @"type":    @"1"}
                   success:^(AFHTTPRequestOperation *operation, id responseObject) {
                       NSLog(@"JSON: %@", [responseObject description]);
                   } 
                   failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                       NSLog(@"Error: %@", [error description]);
                   }
    ];
    
    0 讨论(0)
  • 2020-12-16 15:42

    Just solve my problem, my server was not configured to accept the charset utf8 with application/json so i just removed the charset utf for ajson serializer in Afnetworking 2.0 and now it is working correclty.

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