• AFNetworkingErrorDomain 错误


    AFNetworking and POST Request

    I'm getting this response in error.userInfo while making a POST request from AFNetworking. Can anyone tell either I'm missing anything obvious or something need to fix at my server end?

    Request Failed with Error: Error Domain=AFNetworkingErrorDomain Code=-1016 "Expected content type {( "text/json", "application/json", "text/javascript" )}, got text/html" UserInfo=0x6d7a730 {NSLocalizedRecoverySuggestion=index test, AFNetworkingOperationFailingURLResponseErrorKey=, NSErrorFailingURLKey=http://54.245.14.201/, NSLocalizedDescription=Expected content type {( "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 = "Expected content type {( "text/json", "application/json",
    "text/javascript" )}, got text/html"; NSLocalizedRecoverySuggestion = "index test"; }

    And I'm using this code;

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

    解决办法:

    By default, AFJSONRequestOperation accepts only "text/json", "application/json" or "text/javascript" content-types from server, but you are getting "text/html".

    Fixing on server would be better, but you can also add "text/html" content type as acceptable in your app:

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

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    Android组件化和插件化开发
    开发一流的 Android SDK:Fabric SDK 的创建经验
    关系 和非关系 阻塞非阻塞的区别
    AJAX 中JSON 和JSONP 的区别 以及请求原理
    九马画山数命运, 一身伴君不羡仙! 与代码不离不弃!
    移动端 transitionEnd函数用来检测过渡是否完成
    2017前端该学的知识 分享墨白的文章、大家共勉!
    移动端和pc端事件绑定方式以及取消浏览器默认样式和取消冒泡
    今天要带来的是移动端开发的基础内容
    离线存储
  • 原文地址:https://www.cnblogs.com/zsw-1993/p/4879940.html
Copyright © 2020-2023  润新知