在从AFNetworking
发出POST请求时,我在error.userInfo中收到此响应。
任何人都可以告诉我,我错过了任何明显的东西或需要在我的服务器端修复的东西吗?
请求失败并显示错误:错误域= AFNetworkingErrorDomain 代码= -1016“预期内容类型{( “文/ JSON”, “应用程序/ JSON”, “text / javascript”)},得到text / html“UserInfo = 0x6d7a730 {NSLocalizedRecoverySuggestion = index test, AFNetworkingOperationFailingURLResponseErrorKey =,NSErrorFailingURLKey = http://54.245.14.201/, NSLocalizedDescription =预期内容类型{( “文/ JSON”, “应用程序/ JSON”, “text / javascript”)},得到text / html,AFNetworkingOperationFailingURLRequestErrorKey = http://54.245.14.201/>},{ AFNetworkingOperationFailingURLRequestErrorKey =“http://54.245.14.201/>”; AFNetworkingOperationFailingURLResponseErrorKey =“”; NSErrorFailingURLKey =“http://54.245.14.201/”; NSLocalizedDescription =“预期内容类型{(\ n \”text / json \“,\ n \”application / json \“,\ n
\“text / javascript \”\ n)},得到text / html“; NSLocalizedRecoverySuggestion =“索引测试”; }
我正在使用此代码;
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];
答案 0 :(得分:46)
默认情况下,AFJSONRequestOperation
仅接受来自服务器的“text / json”,“application / json”或“text / javascript”内容类型,但您将获得“text / html”。
修复服务器会更好,但您也可以在应用中添加“text / html”内容类型:
[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
它对我有用,希望这有帮助!
答案 1 :(得分:4)
您是否在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]);
// ...
}
答案 2 :(得分:0)
在此代码中设置您的值并检查它是否适合您
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];