【发布时间】:2015-06-29 19:18:08
【问题描述】:
我必须以以下格式向服务器发送POST 请求:
内容类型:application/x-www-form-urlencodedForm Key : data
表单值:
[
{
"email" : "test@test.com",
"password" : "test@test.com"
}
]
当我在网络休息客户端(邮递员/高级休息客户端)中以这种格式发送请求时,我得到了成功的响应。
那么我怎样才能用AFNetworking 发送这种类型的响应呢?
我的Objective-c 代码是
NSDictionary *dictLogin = @{@"email":@"test@test.com",@"password":@"test@test.com"};
NSDictionary *dictReq = @{@"data":@[dictLogin]};
NSData *data = [NSJSONSerialization dataWithJSONObject:dictReq options:NSJSONWritingPrettyPrinted error:nil];
NSString *strReq = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:@"http://test.php" parameters:dictReq success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
当我将 dictReq 或 str 作为 AFNetworking 参数传递时,我得到了来自服务器的缺少参数/故障响应的响应
如果 AFNetworking 不可能或很难,NSUrlConnection/Request 也可以工作
谢谢
【问题讨论】:
-
您是否确保您的密钥是正确的 JSON 密钥,它们是服务器端预期的密钥?因为上面的 JSON 示例与您在 Objective-c 中的代码生成的不同
-
是的,参数是一样的,你发现两者req有什么区别?在休息客户端中,我将数据键和参数分开,我的代码已将其编写为字典
-
你解决了这个问题吗?你能分享你的解决方案吗?
标签: ios objective-c http-post afnetworking-2 url-encoding