AFNetwoking的默认Content-Type是 application/x-www-form-urlencoded。若服务器要求Content-Type为 applicaiton/json,为了和服务器对应,就必须修改AFNetworking的Content-Type。
简单来说,服务器通过Content-Type来识别传送的数据类型,分辨传送的数据到底是文本、图片或者是其他。如果服务器不识别对应的Content-Type,那么就会返回错误415。
修改Content-Type代码如下:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
修改后便可正常使用如下方法进行POST访问了,GET访问查看头文件选择对应方法即可。
- (AFHTTPRequestOperation *)POST:(NSString *)URLString
parameters:(NSDictionary *)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure