我有一个奇怪的问题...我正在使用 AFNetworking 针对 Rails 服务器执行此 ios http/json 帖子,预期的输出类似于:
{"success":true,"auth_token":"4D8CyUsyGZdqo6X1TCeq"}
有时它会按预期工作,但通常在 Rails 端,请求不会被检测为“JSON”请求,因此它提供 HTML。有人对此有想法吗?在设置 JSON 请求方面我做错了什么吗?
NSDictionary *parameter = @{@"email":@"philswenson@mymail.com", @"password":@"mypassword"};
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://localhost:3000"]];
[httpClient setParameterEncoding:AFJSONParameterEncoding];
[httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
[httpClient postPath:@"api/v1/sessions" parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *jsonString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"Here is what we got %@", jsonString);
NSDictionary *loginResult = [jsonString objectFromJSONString];
NSNumber* success = [loginResult objectForKey:@"success"];
NSLog(@"success = %@", success);
NSLog(@"yay");
// sample output:
// {"success":true,"auth_token":"4D8CyUsyGZdqo6X1TCeq"}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self handleConnectionError:error];
}];