我有 WCF Web 服务,它接受电子邮件地址,如果该条目在数据库中不存在则返回“True”,如果存在则返回“False”。当我使用 fiddler 和 POST 数据服务时,它返回结果为预期,当我尝试从我的 iPad 应用程序发送请求时,它只是说“无效的 JSON 原语:电子邮件”。
服务链接: http://www.unluapps.com/Service1.svc/CheckRejected
用于测试的 JSON:
{"Email":"test@unluco.com"}
iOS 代码:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSError *e = nil;
NSDictionary *parameters = [NSJSONSerialization JSONObjectWithData: [[NSString stringWithFormat:@"{\"Email\":\"%@\"}",_TxtMail.text] dataUsingEncoding:NSUTF8StringEncoding]
options: NSJSONReadingMutableContainers
error: &e
];
[manager POST:@"http://www.unluapps.com/Service1.svc/CheckRejected" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
// NSLog(@"JSON: %@", responseObject);
_receivedData=responseObject;
//NSLog(@"%@",[responseObject objectForKey:@"CheckifExistsResult"]);
NSLog(@"%@",[responseObject objectForKey:@"CheckRejectedResult"]);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}
];
最佳答案
AFHTTPRequestOperationManager 默认使用 AFHTTPRequestSerializer。添加
manager.requestSerializer = [AFJSONRequestSerializer new];
AFHTTPRequestOperationManager 初始化之后。
您还可以简化代码:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer new];
NSDictionary *parameters = @{@"Email": _TxtMail.text};
[manager POST:@"http://www.unluapps.com/Service1.svc/CheckRejected" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
// NSLog(@"JSON: %@", responseObject);
_receivedData=responseObject;
//NSLog(@"%@",[responseObject objectForKey:@"CheckifExistsResult"]);
NSLog(@"%@",[responseObject objectForKey:@"CheckRejectedResult"]);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}
];
关于c# - iOS post json数据到web服务并返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24731920/