嵌套的NSDictionary&NSArray与AFNetworking不能正常工作
NSDictionary *customerDictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"blah@blah.com", @"email", @"1", @"facebook", nil]; NSArray *customerArray = [NSArray arrayWithObjects:customerDictionary, nil]; NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:customerArray, @"customers", nil]; NSURLRequest *request = [sharedHTTPClient requestWithMethod:@"POST" path:@"/api/upload" parameters:parameters]; AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {}]; [operation start];
在Node.JS后端,打印出正文显示:
{ customers: [ '1', 'blah@blah.com' ] }
预期的打印应该是:
{ customers: [{ facebook:'1', email:'blah@blah.com' }] }
我究竟做错了什么?
从AFNetworking wiki:
如何在我的请求中发送JSON参数?
如果您使用的是AFHTTPClient,请将parameterEncoding属性设置为AFJSONParameterEncoding。 该HTTP客户端上具有参数参数的任何方法现在将传递的对象编码为JSONstring,并适当地设置HTTP正文和Content-Type标头。
否则,可以通过添加头Content-Type:application / json,并将请求的主体设置为JSONstring来手动执行此操作。