ios - JSON 文本未以数组或对象开头,并且未设置允许片段的选项

标签 ios json objective-c web-services cocoa-touch

您好,我是 iOS 新手,我正在尝试使用 JSON 从 Web 服务获取响应,但出现以下错误。请帮我解决一下。

Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x7fd30bee0f70 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set., NSUnderlyingError=0x7fd30bede7b0 "Request failed: internal server error (500)"}

-(void)loadFeedWithOffset:(NSInteger)Offset Limit:(NSInteger)Limit
{
     AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

//      [manager.requestSerializer setValue:@"application/json;                 text/html" forHTTPHeaderField:@"Accept"];
//      [manager.requestSerializer setValue:@"application/json;     text/html; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:[[NSUserDefaults standardUserDefaults] objectForKey:@"UID"] forKey:@"user_id"];
    [params setValue:[NSString stringWithFormat:@"%ld",(long)Offset] forKey:@"offset"];
    [params setValue:[NSString stringWithFormat:@"%ld",(long)Limit] forKey:@"limit"];
    [params setValue:[NSString stringWithFormat:@"%d",[AppDelegate sharedAppDelegate].intPostType] forKey:@"post_type"];

    [manager POST:[NSString stringWithFormat:@"%@webservices/post/load", API_URL] parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject)
 {

     NSLog(@"JSON: %@", responseObject);
     if ([[responseObject objectForKey:@"status"] isEqualToString:@"fail"])
     {
         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:[responseObject objectForKey:@"message"] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
         [alert show];
         alert = nil;
     }
     else
     {
         if ([[responseObject objectForKey:@"feed"] count] > 0)
         {
             isOver = FALSE;
             [arrFeed addObjectsFromArray:[responseObject objectForKey:@"feed"]];
             searchedDataArray = [NSMutableArray  arrayWithArray:arrFeed];
             //searchedDataArray=arrFeed;
             [tblMenuDetail reloadData];
         }
         else
         {
             isOver = TRUE;
         }
         [self performSelector:@selector(doneLoadingTableViewData) withObject:self afterDelay:1.0];
     }
     [[AppDelegate sharedAppDelegate] hideProgress];
 } failure:^(AFHTTPRequestOperation *operation, NSError *error)
 {
     [[AppDelegate sharedAppDelegate] hideProgress];
     NSLog(@"Error: %@", error);
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
     [alert show];
     alert = nil;
 }];
}

最佳答案

这通常是后端相关的问题,json 格式不正确,您在客户端对此无能为力....

虽然有时这种情况会发生在向后端发送错误的参数时,因此您可以检查所有参数是否正确(包括类型和值)

关于ios - JSON 文本未以数组或对象开头,并且未设置允许片段的选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33951401/

相关文章:

ios - 当音频设备添加到 AVCaptureSession 时,UIImpactFeedbackGenerator 不工作

ios - iOS6会收到静默推送通知吗?

iphone - 图像的圆角

javascript - 无法使用 JavaScript 解析器解析 JSON。方括号

javascript - 如何修改/重新转换 JSON 数组结构

ios - ReactiveCocoa 将单个信号转换为值

ios - 如何为 UITableView 部分页脚设置动画

json - 反序列化与 Symfony Serializer Component 有关系的实体

objective-c - NSAlert 中的 NSTextView 没有滚动条

objective-c - 如何将子类声明为父类的属性以在相关子类中使用?