许多地方已经回答了这个问题,但是没有解决方案对我有用!我正在使用AFNetworking的代码,如下所示

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer=[AFJSONResponseSerializer serializer];
NSDictionary *parameters=@{@"Key1":@"Value1",@"Key2":@"Value2"};
//    NSDictionary *parameters = @{@"foo": @"bar"};
[manager POST:@"https://www.MyURL.com/index.php" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

错误:
Error: Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed. (NSURLErrorDomain error -1012.)" UserInfo=0x7a2a03b0 {NSErrorFailingURLKey=https://www.MyURL.com/index.php, NSErrorFailingURLStringKey=https://www.MyURL.com/index.php}

我没有上述要求的运气。

我不知道POST请求中的代码出了什么问题,GET请求在AFNetworking中工作正常。

最佳答案

试试这个:

 NSDictionary *dictParameters = parameter here

 //create url
 NSString *strURL = [NSString stringWithFormat:@"url here"];
 NSLog(@"loginurl : %@",strURL);

 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

 manager.responseSerializer = [AFHTTPResponseSerializer serializer];

 AFHTTPRequestOperation *apiRequest = [manager POST:strURL parameters:dictParameters success:^(AFHTTPRequestOperation *operation, id responseObject)
 {
    NSData *jsonData = (NSData *)responseObject;

    NSError * parsedError = nil;

    id *value = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&parsedError];
    if (parsedError == nil)
    {
       //Successfull
    }
    else
    {
       NSLog(@"wrong while parsing json data");
    }
 }
 failure:^(AFHTTPRequestOperation *operation, NSError *error)
 {
      NSLog(@"Error : %@",[error description]);
 }];

 //start request right now
 [apiRequest start];

编辑:刚格式化

关于ios - AFNetworking POST请求不起作用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26361291/

10-13 03:45