Ios 错误域=NSCOCAERRORDOMAIN代码=3840“;字符0周围的值无效。”;UserInfo={NSDebugDescription=字符0周围的值无效。}

Ios 错误域=NSCOCAERRORDOMAIN代码=3840“;字符0周围的值无效。”;UserInfo={NSDebugDescription=字符0周围的值无效。},ios,objective-c,Ios,Objective C,我正在尝试通过将图像转换为base64格式来上载图像。我的错误就在下面 错误域=NSCOCAERRORDOMAIN Code=3840“字符0周围的值无效。”UserInfo={NSDebugDescription=字符0周围的值无效。} 请参考我的密码 NSData *imageData = UIImagePNGRepresentation(image); NSString *imageDataString = [imageData base64EncodedString]; 下面是Post

我正在尝试通过将图像转换为base64格式来上载图像。我的错误就在下面

错误域=NSCOCAERRORDOMAIN Code=3840“字符0周围的值无效。”UserInfo={NSDebugDescription=字符0周围的值无效。}

请参考我的密码

NSData *imageData = UIImagePNGRepresentation(image);
NSString *imageDataString = [imageData base64EncodedString];
下面是Post请求方法

- (id) postRequest:(NSURL *)postURL postString:(NSString *)postString 
{
    NSError * error=nil;
    NSURLResponse * urlResponse;
    NSData *myRequestData = [ NSData dataWithBytes: [ postString UTF8String ] length: [ postString length ]];
    NSMutableURLRequest * request =[[NSMutableURLRequest alloc]initWithURL:postURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
    [request setHTTPBody: myRequestData];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

    NSData * data =[NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
    if (!data)
    {
        return nil;
    }

    id jsonnResponse =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
    return jsonnResponse;
}

这行代码中的问题是,
NSJSONSerialization
无法解析您的响应

id jsonnResponse =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
该错误表明服务器的响应不正确

来自服务器的响应必须是具有顶级容器(如数组或字典)的有效JSON。查看您的回复

错误3840。。。字符0周围的值无效

这意味着(JSON)字符串是空的,您从服务器上什么也得不到

要从字符串中获取
NSData
,有一个更方便的API:

NSData *myRequestData = [postString dataUsingEncoding:NSUTF8StringEncoding];
实际上,base64格式的字符串不是标头中指定的JSON。这可能会导致问题

PS:不要使用同步–已弃用–API通过网络加载数据