美文网首页
2020-07-30 ios post请求方式表单与Json

2020-07-30 ios post请求方式表单与Json

作者: 缘來諟夢 | 来源:发表于2020-08-05 15:11 被阅读0次

    最近遇到的坑,两个后台,一个表单,一个Json格式,之前一直没搞明白
    附上源码供参考

    // 1.创建一个请求管理者
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        //添加多的请求格式
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"application/x-www-form-urlencoded",@"text/plain",@"text/json", @"text/javascript",@"text/html",nil];
        if (![URL containsString:@"##"]) {
            //申明请求的数据是json类型,两种写法
            manager.requestSerializer=[AFJSONRequestSerializer serializer];
            //    [manager.requestSerializer setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
        }
        else
        {
            //默认是表单
            //    [manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        }
        URL = [URL componentsSeparatedByString:@"##"][0];
        NSLog(@"Url-->%@,\nParam-->%@",URL,parameters);
        [manager POST:URL parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
            //            progress(uploadProgress);
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            [SVProgressHUD dismiss];
            if (success) {
                success(responseObject);
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [SVProgressHUD dismiss];
            if (failure) {
                failure(error);
            }
        }];
    

    相关文章

      网友评论

          本文标题:2020-07-30 ios post请求方式表单与Json

          本文链接:https://www.haomeiwen.com/subject/ywjrrktx.html