2017-08-02 52 views
0

我有一個用戶的表單。填充後,用戶點擊提交按鈕,其數據提交給服務器。我收到一個問題,我得到的響應爲空,但我的表單中的數據已成功提交給服務器。我不知道問題出現在哪裏。如果響應成真,我想運行另一個函數。我的代碼是這樣的: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); ' –

回答

1

請在您的瀏覽器郵遞員或RESTClient實現試和調試您的POST請求,有可能是服務器沒有向您發送適當的反應。你可以從這裏下載https://restlet.com