2012-11-07 137 views
4

我正在使用AFNetworking为我的REST服务(WCF)。这里是代码:AFNetworking JSON请求到WCF REST服务

NSDictionary *userName = [NSDictionary dictionaryWithObject:@"user" forKey:@"UserNameOrEmail"]; 
    NSDictionary *pass = [NSDictionary dictionaryWithObject:@"123" forKey:@"Password"]; 
    NSArray *credentials = [NSArray arrayWithObjects:userName,pass,nil]; 
    NSDictionary *params = [NSDictionary dictionaryWithObject:credentials forKey:@"request"]; 


    AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL: 
    [NSURL URLWithString:@"http://server"]]; 

    [client postPath:@"/ProfileWebService.svc/login" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { 
    NSString *text = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]; 
    NSLog(@"Response: %@", text); 
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    NSLog(@"%@", [error localizedDescription]); 
}]; 

但我得到400 HTTP错误。

使用HTML和AJAX,它看起来像:

 $(document).ready(function() { 

      $("#login_call").click(function() { 
       $.ajax({ 
        type: 'POST', 
        url: 'http://server/ProfileWebService.svc/login', 
        contentType : 'application/json', 
        dataType: 'json', 
        data: JSON.stringify({request: {UserNameOrEmail: $('#login_username').val(), Password: $('#login_password').val()}}), 
        success: function (data) { 
         $('#login_result').val('Code: ' + data.LoginResult.Code + '\nFault String: ' + data.LoginResult.FaultString); 
        } 
       }); 
      }); 

     }); 

和工作正常。

参数有什么问题。

+0

更详细的AFNetworking文档研究给了我一个答案,我应该只添加一行代码 client.parameterEncoding = AFJSONParameterEncoding; 就是这样! – matr0sk1n

+0

你可以发表您的评论作为答案并接受它吗? – carlosfigueira

+0

我没有足够的业力,只有在我问这个问题后的6个小时内。 – matr0sk1n

回答

3

更详细的AFNetworking文档学习上搜索#1给了我一个SOLUTION

1.Parameters的要求应该是这样的:

NSDictionary *params = 
     [NSDictionary dictionaryWithObjectsAndKeys: 
      [NSDictionary dictionaryWithObjectsAndKeys: 
          username.text, @"UserNameOrEmail", 
          password.text, @"Password", 
          nil], 
     @"request", 
     nil]; 

2.创建AFHTTPClient

AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL: 
     [NSURL URLWithString:@"http://server"]]; 

3.当我发送JSON作为参数时,我必须添加:

client.parameterEncoding = AFJSONParameterEncoding; 

我这样做后,我摆脱了404错误,但我不能做任何与JSON响应,我不知道为什么。但是该解决方案是:

4.Creating的请求:

NSMutableURLRequest *request = 
    [client requestWithMethod:@"POST" path:@"/ProfileWebService.svc/login" parameters:params]; 

5.Creating操作:

AFJSONRequestOperation *operation = 
    [AFJSONRequestOperation JSONRequestOperationWithRequest:request 
    success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) 
    { 
    NSLog(@"JSON: %@", [JSON valueForKeyPath:@"LoginResult"]); 
    NSLog(@"Code: %@", [[[JSON valueForKeyPath:@"LoginResult"] valueForKeyPath:@"Code"] stringValue]); 
    NSLog(@"FaultString: %@", [[JSON valueForKeyPath:@"LoginResult"] valueForKeyPath:@"FaultString"]); 
    } 
    failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) 
    { 
    NSLog(@"error opening connection"); 
    }]; 

6.启动操作:

[operation start]; 

希望有的AFNetworking初学者发现这个帖子很有帮助。