1

AFJSONOperationRequestを使用する前にNSMutableURLRequestを使用していますが、Railsアプリでデータを取得する際に問題が発生します。

私が使用する場合:

  NSMutableURLRequest *request = [[HTTPClient sharedClient] multipartFormRequestWithMethod:@"POST" path:path parameters:dict constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) 
  {
  }]; 

それで :

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) 
  { 
    else {
    }
  } 

Railsログでデータが正しくフォーマットされています。

Parameters: {"contact"=>{"country_id"=>"45", "lastname"=>"Tutu"}}

しかし、ファイルを送信するのにAFMultipartFormDataは必要ありません...(送信するファイルはありません)

したがって、代わりに、次を使用します。

  NSMutableURLRequest *request = [[HTTPClient sharedClient] multipartFormRequestWithMethod:@"POST" path:path parameters:dict];

私のAFJSONRequestOperationでも。しかし、私のパラメータは私のRailsアプリで正しく設定されていません:

Parameters: {contact[country_id] => "45", contact[lastname] => "Tutu"} 

それ以外の

Parameters: {"contact"=>{"country_id"=>"45", "lastname"=>"Tutu"}}

理由がわかりません。ブロック「constructingBodyWithBlock」を使用しないと、リクエストの本文が正しく設定されていないようです。

4

1 に答える 1

0

multipartFormRequestWithMethod を使用しないでください。

このようにpostPathメソッドを使用するだけです

[[YourHTTPClient sharedClient] postPath:path
   parameters:dict
      success:^(AFHTTPRequestOperation *operation, NSString* path){
      }
      failure:^(AFHTTPRequestOperation *operation, NSError *error) {
      }];

編集:

操作が必要な場合は、AFJSONRequestOperation を直接使用します

+ (AFJSONRequestOperation *)JSONRequestOperationWithRequest:(NSURLRequest *)urlRequest
                                                success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, id JSON))success 
                                                failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON))failure;


NSURL *url = [NSURL URLWithString:@"http://yoursite.com/path"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation * operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
                                                success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
                                                }
                                                failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
                                                }];
于 2012-03-06T10:54:33.280 に答える