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);
}
});
});
});
和工作正常。
參數有什麼問題。
更詳細的AFNetworking文檔研究給了我一個答案,我應該只添加一行代碼 client.parameterEncoding = AFJSONParameterEncoding; 就是這樣! – matr0sk1n
你可以發表您的評論作爲答案並接受它嗎? – carlosfigueira
我沒有足夠的業力,只有在我問這個問題後的6個小時內。 – matr0sk1n