私は奇妙な問題を抱えています...レールサーバーに対してAFNetworkingを使用してこのios http/json投稿を行っていますが、予想される出力は次のようなものです:
{"success":true,"auth_token":"4D8CyUsyGZdqo6X1TCeq"}
期待どおりに動作することもありますが、多くの場合、レール側ではリクエストが「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];
}];