我将 AFNetworking 用于我的 REST 服务 (WCF)。这是代码:
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);
}
});
});
});
并且工作正常。
参数有什么问题。