AFNetworkingを使用してサーバーにPOSTリクエストを送信しようとしていますが、すべてが機能しているようです。つまり、アプリケーションがサーバーに正常にpingを実行しています。ただし、デバッガーを使用して以下のコードをステップ実行した後、値が正常に渡されたように見えても、サーバーに到達すると、送信されるパラメーター値は空白になります。これに関する助けをいただければ幸いです。
APIClient.m
#import "APIClient.h"
#import "AFJSONRequestOperation.h"
// Removed URL for privacy purposes.
static NSString * const kAPIBaseURLString = @"string goes here";
@implementation APIClient
+ (APIClient *)sharedClient {
static APIClient *_sharedClient;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedClient = [[APIClient alloc] initWithBaseURL:[NSURL URLWithString:kAPIBaseURLString]];
});
return _sharedClient;
}
- (id)initWithBaseURL:(NSURL *)url {
self = [super initWithBaseURL:url];
if (self) {
[self registerHTTPOperationClass:[AFJSONRequestOperation class]];
// Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
[self setDefaultHeader:@"Accept" value:@"application/json"];
}
return self;
}
@end
LoginBrain.mのログイン方法
- (void)loginUsingEmail:(NSString *)email andPassword:(NSString *)password withBlock:(void (^)(NSDictionary *loginResults))block {
self.email = email;
self.password = password;
// Removed path for privacy purposes
[[APIClient sharedClient] postPath:@"insert path here" parameters:[NSDictionary dictionaryWithObjectsAndKeys:email, @"uname", password, @"pw", nil] success:^(AFHTTPRequestOperation *operation, id responseJSON) {
if (block) {
block(responseJSON);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
if (block) {
block(nil);
}
}];
// Store user data in app?
}
LoginViewController.mのLoginCalledメソッド
- (IBAction)loginPressed {
[self.loginProgressIndicator startAnimating];
NSString *email = self.emailTextField.text;
NSString *password = self.passwordTextField.text;
[self.brain loginUsingEmail:email andPassword:password withBlock:^(NSDictionary *loginResults) {
[self.loginProgressIndicator stopAnimating];
[self.delegate uloopLoginViewController:self didLoginUserWithEmail:email andPassword:password];
}];
}
アップデート
ここparameterEncoding
で推奨されているように変更してみましたが、問題は解決しませんでした。
2回目の更新
これは、POSTデータにアクセスしているサーバー側からのPHPコードです。私はサーバー側で何もしておらず、その動作に非常に慣れていないため、これは私の同僚によって書かれました。
header('Content-type: application/json');
$username = $_POST['uname'];
$pw = $_POST['pw'];
サーバーコードは非常に単純です。彼は、変数値が何であるかを確認するある種のログスクリプトを持っており、クライアントがサーバーにアクセスしていると言っていますが、変数値は空白です。
3番目の更新
print_r
これは、$_REQUEST
変数のを生成することによるHTTPリクエストのダンプです。
Array ( [sid] => FwAqvZrfckw )
そして、これが$_POST
変数のダンプです。ご覧のとおり、完全に空白です。
Array ( )
4番目の更新
サーバーに送信される前にWiresharkを使用してパケットをキャプチャしましたが、すべてが正常に表示されています。
Accept: application/json
Content-Type: application/x-www-form-urlencoded; charset=utf-8
そして、POST
パラメータもすべてそこにありました。また、サーバー側でテストファイルを作成し、POST
そこにあるコードが機能していることを確認するためのテストを実行しました。