2012-11-07 133 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初學者發現這個帖子很有幫助。