Post Request with AFHttpRequestOperationManager not working

前端 未结 2 1835
名媛妹妹
名媛妹妹 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]);
                   }
    ];
    

提交回复
热议问题