IOS中的POST方法问题
我有一个用户表格。填充后,用户单击submit按钮并将其数据提交到服务器。我遇到一个问题,我得到的响应为null,但我的表单数据已成功提交到服务器。我不知道问题出在哪里。如果响应为真,我想运行另一个函数。我的代码是:IOS中的POST方法问题,ios,objective-c,nsurlsession,Ios,Objective C,Nsurlsession,我有一个用户表格。填充后,用户单击submit按钮并将其数据提交到服务器。我遇到一个问题,我得到的响应为null,但我的表单数据已成功提交到服务器。我不知道问题出在哪里。如果响应为真,我想运行另一个函数。我的代码是: NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *defaultSessio
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];
NSString *urlLinkA=@"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 *msg=@"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];
}
请尝试在浏览器中使用postman或RestClient并调试您的post请求,可能是服务器没有向您发送正确的响应。您可以从这里下载。是否在应用程序中添加了NSTransport安全性是@Anbu,Karthikwhat结果如何NSLog@Response:%@%@\n,响应,错误;为什么不打印错误消息?请尝试在浏览器中使用postman或RestClient并调试您的post请求,可能是服务器没有向您发送正确的响应。