以下是我向服务器发送请求的方式:
- (void)searchForText:(NSString *)searchText scope:(NSInteger)scopeOption
{
[SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
[SVProgressHUD showWithStatus:@"Getting Data"];
NSString *string = [NSString stringWithFormat:@"%@/API/ICD10Code/1",BaseURLString];
NSDictionary *para = @{@"Code":searchText};
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSMutableURLRequest *req = [[AFJSONRequestSerializer serializer] requestWithMethod:@"PUT" URLString:string parameters:para error:nil];
NSString *token = [NSString stringWithFormat:@"Bearer %@",[[NSUserDefaults standardUserDefaults] objectForKey:@"userToken"]];
[req setValue:token forHTTPHeaderField:@"Authorization"];
NSLog(@"URL is: %@ Parameters: %@ Token: %@",string, para, token);
[[manager dataTaskWithRequest:req completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error){
if (!error) {
if (response) {
NSLog(@"Response is: %@",response);
[SVProgressHUD dismiss];
}
} else {
// NSLog(@"Error: %@, %@, %@", error, response, responseObject);
NSLog(@"Error: %@", error.localizedDescription);
[SVProgressHUD dismiss];
SCLAlertView *alert = [[SCLAlertView alloc] init];
[alert showError:self title:@"Error"
subTitle:[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"Message"]]
closeButtonTitle:@"OK" duration:0.0f];
}
}] resume];
}
当我运行代码时,响应的NSLog
给了我:
回复是:{URL: http://ts-test01/app/API/ICD10Code/1} {状态代码:200,headers { “Cache-Control”=“no-cache”; “内容长度”= 1525; “Content-Type”=“application / json; charset = utf-8”; Date =“Wed,05 Oct 2016 09:50:15 GMT”; Expires =“ - 1”; Pragma =“no-cache”; Server =“Microsoft-IIS / 8.0”; “X-AspNet-Version”=“4.0.30319”; “X-Powered-By”=“ASP.NET”; }}
但后端人正在获取数据。发送请求时我做错了什么。
答案 0 :(得分:2)
您错误地打印了回复,请使用responseObject
获取输出
在这个地方
NSLog(@"Response is: %@",response);
使用
NSLog(@"output== %@ ", responseObject);
表示样本
[[manager dataTaskWithRequest:req completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error){
[SVProgressHUD dismiss];
if (!error) {
if (response) {
NSLog(@"output== %@", responseObject);
if ([responseObject isKindOfClass:[NSDictionary class]]) {
//blah blah
}
}
} else {
// NSLog(@"Error: %@, %@, %@", error, response, responseObject);
NSLog(@"Error: %@", error.localizedDescription);
SCLAlertView *alert = [[SCLAlertView alloc] init];
[alert showError:self title:@"Error"
subTitle:[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"Message"]]
closeButtonTitle:@"OK" duration:0.0f];
}
}] resume];