NSDebugDescription = JSON文本没有以数组或对象开头,并且选项允许未设置片段 - iOS

时间:2014-11-27 16:13:14

标签: ios objective-c

我尝试使用它将用户信息+图像上传到数据库:

- (void)Inscription_withImage:(NSArray *)value :(NSData *)data completion:(void (^)( NSString * retour))block{


    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];

    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"data[User][username]"] = @"Jack";
    params[@"data[User][password]"] = @"Jack";
    params[@"data[User][email]"] = @"Jack";
    params[@"data[User][first_name]"] = @"Jack";
    params[@"data[User][last_name]"] = @"Jack";
    mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [mgr POST:@"http://  /Messenger/services/messenger_register" parameters:params
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {


    [formData appendPartWithFileData:data name:@"data[User][image]" fileName:@"image.jpg" mimeType:@"image/jpg"];
     }
      success:^(AFHTTPRequestOperation *operation, id responseObject) {
          NSLog(@"upload success!----%@", responseObject);
      } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          NSLog(@"upload failed!----%@", error);
      }];


  }

当我添加这个:

[formData appendPartWithFileData:data name:@"data[User][image]" fileName:@"image.jpg" mimeType:@"image/jpg"];

我发现了这个问题:

上传失败!----错误Domain = NSCocoaErrorDomain Code = 3840“操作无法完成。(Cocoa error 3840.)”(JSON文本没有以数组或对象开头,并且选项允许未设置片段。)UserInfo = 0x176d25c0 {NSDebugDescription = JSON文本没有以数组或对象开头,并且选项允许未设置片段。}

我需要你的帮助

1 个答案:

答案 0 :(得分:0)

应将Image转换为NSData,然后在base64中编码以压缩其大小

 NSData * representation = UIImageJPEGRepresentation(image, 1.0f);
    NSString * imageBase64 =[NSString base64StringFromData:representation];
    NSDictionary* parameters = [NSDictionary dictionaryWithObjectsAndKeys:
                                userID,@"id",
                                imageBase64,@"img_data",
                                [NSString stringWithFormat:@"user_%@.jpg",userID],@"avatar_url",nil];