当我使用AFNetworking进行POST时,有没有人知道我为什么会这样做(错误):
[ '0',
'1',
'7',
'0',
'2',
'94',
'0',
'3',
'41',
'0',
'4',
'5',
'0',
'5',
'51',
'0',
'6',
'3',
'0',
'7',
'40',
'0',
'8',
'18',
'0',
'9',
'12',
'0',
'10',
'10',
'1',
'11',
'9',
'2',
'12',
'4',
'2',
'13',
'17',
'2',
'14',
'25',
'2',
'15',
'16',
'2',
'16',
'15',
'2',
'17',
'21',
'2',
'18',
'35',
'2',
'19',
'37',
'2',
'20',
'34',
'2',
'21',
'32',
'3',
'22',
'96' ]
但是当我用邮递员发帖时我得到了这个(正确)?
[ { riderId: 7, placeId: 1, eventCategoryId: 0 },
{ riderId: 94, placeId: 2, eventCategoryId: 0 },
{ riderId: 41, placeId: 3, eventCategoryId: 0 },
{ riderId: 5, placeId: 4, eventCategoryId: 0 },
{ riderId: 51, placeId: 5, eventCategoryId: 0 },
{ riderId: 3, placeId: 6, eventCategoryId: 0 },
{ riderId: 40, placeId: 7, eventCategoryId: 0 },
{ riderId: 18, placeId: 8, eventCategoryId: 0 },
{ riderId: 12, placeId: 9, eventCategoryId: 0 },
{ riderId: 10, placeId: 10, eventCategoryId: 0 },
{ riderId: 9, placeId: 11, eventCategoryId: 1 },
{ riderId: 4, placeId: 12, eventCategoryId: 2 },
{ riderId: 17, placeId: 13, eventCategoryId: 2 },
{ riderId: 25, placeId: 14, eventCategoryId: 2 },
{ riderId: 16, placeId: 15, eventCategoryId: 2 },
{ riderId: 15, placeId: 16, eventCategoryId: 2 },
{ riderId: 21, placeId: 17, eventCategoryId: 2 },
{ riderId: 35, placeId: 18, eventCategoryId: 2 },
{ riderId: 37, placeId: 19, eventCategoryId: 2 },
{ riderId: 34, placeId: 20, eventCategoryId: 2 },
{ riderId: 32, placeId: 21, eventCategoryId: 2 },
{ riderId: 96, placeId: 22, eventCategoryId: 3 } ]
这是一个node.js响应btw。在两者上发布相同的端点和json数据。
思想?
它只是一个简单的NSMutableDictionary,我已经过去了。这是我的AFNetworking提交代码:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:@"http://192.168.20.230:3000/api/v1/test" parameters:submission success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"responseObject: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", [error localizedDescription]);
}];
答案 0 :(得分:0)
想出来。显然我需要设置requestSerializer。
manager.requestSerializer = [AFJSONRequestSerializer serializer];
我认为json是AFNetworking的默认设置,所以设置是多余的。好吧。
完整的工作代码:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager POST:@"http://192.168.20.230:3000/api/v1/test" parameters:submission success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"responseObject: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", [error localizedDescription]);
}];