Solicitud de AFNetworking y POST

AFNetworking esta respuesta en error.userInfo mientras AFNetworking una request POST desde AFNetworking . ¿Alguien puede decir que me falta algo obvio o algo que necesite arreglar en mi server final?

Error de request con error: Error Domain = AFNetworkingErrorDomain Code = -1016 "Tipo de contenido esperado {(" text / json "," application / json "," text / javascript ")}, got text / html" UserInfo = 0x6d7a730 {NSLocalizedRecoverySuggestion = índice de testing, AFNetworkingOperationFailingURLResponseErrorKey =, NSErrorFailingURLKey = http: //54.245.14.201/, NSLocalizedDescription = Tipo de contenido esperado {("text / json", "application / json", "text / javascript")}, got text / html, AFNetworkingOperationFailingURLRequestErrorKey = http: //54.245.14.201/>}, {AFNetworkingOperationFailingURLRequestErrorKey = "http://54.245.14.201/>"; AFNetworkingOperationFailingURLResponseErrorKey = ""; NSErrorFailingURLKey = "http://54.245.14.201/"; NSLocalizedDescription = "Tipo de contenido esperado {(\ n \" text / json \ ", \ n \" application / json \ ", \ n
\ "text / javascript \" \ n)}, recibió text / html "; NSLocalizedRecoverySuggestion =" índice de testing ";}

Y estoy usando este código;

 AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; [httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]]; [httpClient setDefaultHeader:@"Accept" value:@"application/json"]; NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: @"Ans", @"name", @"29", @"age", nil]; NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"/" parameters:params]; AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { NSLog(@"Success"); NSLog(@"%@",JSON); } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo); NSLog(@"Failure"); }]; [operation start]; [operation waitUntilFinished]; 

Por defecto, AFJSONRequestOperation solo acepta "text / json", "application / json" o "text / javascript" content-types del server, pero obtienes "text / html".

La reparación en el server sería mejor, pero también puede agregar el tipo de contenido "text / html" como aceptable en su aplicación:

 [AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]]; 

¡Funcionó para mí, espero que esto ayude!

¿ AFHTTPClient esta request POST por AFHTTPClient ? Si es así, debe establecer la class de operación para ello:

 AFHTTPClient * client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://localhost:8080"]]; // ... [client registerHTTPOperationClass:[AFJSONRequestOperation class]]; [client setDefaultHeader:@"Accept" value:@"application/json"]; // ... // EDIT: Use AFHTTPClient's POST method NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: @"Ans", @"name", @"29", @"age", nil]; // POST, and for GET request, you need to use |-getPath:parameters:success:failure:| [client postPath:@"/" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"RESPONSE: %@", responseObject); // ... } failure:^(AFHTTPRequestOperation *operation, NSError *error) { if (error) NSLog(@"%@", [error localizedDescription]); // ... } 

Configura tus valores en este código y comtesting si te funciona.

  AFHTTPClient *httpClient = [[AFHTTPClient alloc]initWithBaseURL:[NSURL URLWithString:kBASEURL]]; NSString *_path = [NSString stringWithFormat:@"groups/"]; _path = [_path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSLog(@"%s %@",__PRETTY_FUNCTION__,_path); NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:_path parameters:postParams]; [httpClient release]; AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { if ([JSON isKindOfClass:[NSArray class]] || [JSON isKindOfClass:[NSDictionary class]]) { completed(JSON); } else { } [[UIApplication shanetworkingApplication] setNetworkActivityIndicatorVisible:NO]; } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { NSLog(@" response %@ \n error %@ \n JSON %@",response,error,JSON); [[UIApplication shanetworkingApplication] setNetworkActivityIndicatorVisible:NO]; erronetworking(error); }]; NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease]; [queue addOperation:operation]; [[UIApplication shanetworkingApplication] setNetworkActivityIndicatorVisible:YES];