IOS中的POST方法问题

问题描述:

我有一个用户的表单。填充后,用户点击提交按钮,其数据提交给服务器。我收到一个问题,我得到的响应为空,但我的表单中的数据已成功提交给服务器。我不知道问题出现在哪里。如果响应成真,我想运行另一个函数。我的代码是这样的:IOS中的POST方法问题

NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration]; 
    NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]]; 

    NSString *[email protected]"My URL"; 


    NSURL * url = [NSURL URLWithString:urlLinkA]; 

    NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url]; 

    NSString *parameters = [NSString stringWithFormat:@"%@",Parameter]; 


    NSLog(@"parameter %@",parameters); 
    [urlRequest setHTTPMethod:@"POST"]; 

    [urlRequest setHTTPBody:[parameters dataUsingEncoding:NSUTF8StringEncoding]]; 

    NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest 
                 completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
                  [_indicator stopAnimating]; 
                  _indicator.hidden=YES; 
                  NSLog(@"Response:%@ %@\n", response, error); 

                  NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error]; 

                  NSLog(@"DDD %@",dictionary); 

                  NSString *userid = [dictionary valueForKey:@"property_id"]; 
                  NSLog(@"UserID: %@", userid); 



                  NSLog(@"Response: %@", dictionary); 

                  if ([dictionary isEqual:@""]) { 
                   [self Images]; 
                  } 

                  HomePageViewController *presales = [self.storyboard instantiateViewControllerWithIdentifier:@"HomeViewController"]; 
                  [self.navigationController pushViewController:presales animated:YES]; 

                  NSString *[email protected]"Successfully Registered"; 

                  UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Success" 
                                  message:msg 
                                preferredStyle:UIAlertControllerStyleAlert]; 

                  [self presentViewController:alert animated:YES completion:nil]; 

                  int duration = 2; // duration in seconds 

                  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ 
                   [alert dismissViewControllerAnimated:YES completion:nil]; 
                  }); 

                 }]; 


    NSLog(@"network error :"); 
    [dataTask resume]; 
} 
+1

是否在您的应用中添加了NSTransport安全性 –

+0

是的。 @ Anbu.Karthik –

+0

你在这里得到什么结果NSLog(@“Response:%@%@ \ n”,response,error); ' –

请在您的浏览器邮递员或RESTClient实现试和调试您的POST请求,有可能是服务器没有向您发送适当的反应。你可以从这里下载https://restlet.com