AFNetworking和POST请求

在从AFNetworking发出POST请求时,我在error.userInfo中得到了这个响应。 任何人都可以告诉我缺less任何明显的东西或需要修复我的服务器端?

请求失败,错误:错误域= AFNetworkingErrorDomain代码= -1016“预期的内容types{(”文本/ JSON“,”应用程序/ JSON“,”文本/ JavaScript“)),得到文本/ HTML”UserInfo = 0x6d7a730 {NSLocalizedRecoverySuggestion = AFNetworkingOperationFailingURLRequestErrorKey = NSErrorFailingURLKey = http://54.245.14.201/,NSLocalizedDescription =预期内容types{(“text / json”,“application / json”,“text / javascript”)},获取文本/ html,AFNetworkingOperationFailingURLRequestErrorKey = http://54.245.14.201/>},{AFNetworkingOperationFailingURLRequestErrorKey =“http://54.245.14.201/”“; AFNetworkingOperationFailingURLResponseErrorKey =“”; NSErrorFailingURLKey =“http://54.245.14.201/”; NSLocalizedDescription =“期望的内容types{(\ n \”text / json \“,\ n \”application / json \“,\ n
\“text / javascript \”\ n)},得到text / html“; NSLocalizedRecoverySuggestion =”index test“;}

我正在使用这个代码;

 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]; 

默认情况下, AFJSONRequestOperation只接受来自服务器的“text / json”,“application / json”或者“text / javascript”内容types,但是你会得到“text / html”。

固定在服务器上会更好,但你也可以添加“text / html”内容types在你的应用程序中可接受的:

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

它为我工作,希望这有助于!

您是否通过AFHTTPClient发送了这个POST请求? 如果是这样,你需要设置它的操作类:

 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]); // ... } 

在这个代码中设置你的值,并检查它是否适合你

  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 sharedApplication] setNetworkActivityIndicatorVisible:NO]; } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { NSLog(@" response %@ \n error %@ \n JSON %@",response,error,JSON); [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; errored(error); }]; NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease]; [queue addOperation:operation]; [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];