c# - iOS post json数据到web服务并返回结果

标签 c# ios json wcf afnetworking

我有 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/

相关文章:

c# - [,]有什么用?

ios - Tableview subview 覆盖UIView子类

ios - ShareKit 导致我的 iOS 应用程序崩溃?

ios - 不同 iOS iPhone 尺寸的全屏图像

javascript - 使用 Newtonsoft.Json 删除引号转义

c# - 如何让 IntPtr 访问 MemoryMappedFile 的 View ?

c# - 两个线程使用相同的变量,会产生问题

c# - 微软 MVC : Is there a way to separate C# code from markup in MVC views?

java - 我目前正在为最后一年开发一个应用程序。没有出现错误,但是当我选择我的 searchScreen 应用程序崩溃

javascript - 如何从 URL 中提取 JSON?