AFNetworking和POST请求

Ans*_*Ans 14 ios afnetworking

我在发出POST请求时在error.userInfo中收到此响应AFNetworking.任何人都可以告诉我,我错过任何明显或需要修复我的服务器端的东西吗?

请求失败并显示错误:错误域= AFNetworkingErrorDomain代码= -1016"预期内容类型{("text/json","application/json","text/javascript")},得到text/html"UserInfo = 0x6d7a730 {NSLocalizedRecoverySuggestion = index test,AFNetworkingOperationFailingURLResponseErrorKey =,NSErrorFailingURLKey = http://54.245.14.201/,NSLocalizedDescription =预期内容类型{("text/json","application/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];
Run Code Online (Sandbox Code Playgroud)

tar*_*lop 46

默认情况下,AFJSONRequestOperation只接受"文字/ JSON","应用/ JSON"或"文/ JavaScript的"内容类型的服务器,但你得到"text/html的".

修复服务器会更好,但您也可以在应用中添加"text/html"内容类型:

[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
Run Code Online (Sandbox Code Playgroud)

它对我有用,希望这有帮助!