我必须通过发送照片字节将图像上传到服务器,并且我正在使用 AFNetworking,
但我不断收到这个错误-
Error: 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=0x10abb7350 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
最佳答案
如果 PHP 服务器没有响应 application/json
,那么你必须告诉你的manager
接受一般 HTTP 响应:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:path parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData name:@"image" fileName:imageFilename mimeType:@"image/png"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"Success: %@", string);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
如果您的服务器响应 JSON 响应,那么它可能没有设置
Content-Type
header 正确,例如 header("Content-type: application/json");
.
关于ios - 使用 AFNetworking、ios 上传图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19966941/