postgresql (Cocoa 错误 3840。)”(JSON 文本未以数组或对象开头,并且没有设置允许片段的选项。)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20648854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:13:57  来源:igfitidea点击:

(Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.)

djangojsonpostgresqlafnetworking-2

提问by Kukula Mula

I find this error to happen to many other users and although trying many of suggested solutions nothing seems to work, so I'm trying to post my specific case.

我发现这个错误发生在许多其他用户身上,虽然尝试了许多建议的解决方案似乎没有任何效果,所以我试图发布我的具体案例。

I'm trying to save an image from iphone application to my postgresql database using django.

我正在尝试使用 django 将 iphone 应用程序中的图像保存到我的 postgresql 数据库中。

my view looks like this:

我的观点是这样的:

def objectimages(request, obj_id):
    object = imagesTable.objects.filter(uniqueid=obj_id)
    if request.method == 'POST':
        value = request.POST.get('image')
        f= open('image.txt', 'w+')
        f.write(value)
        f.close()
        object.update(image=value)
        return HttpResponse("Post received")
    elif request.method == 'GET':
        output = serializers.serialize('json',object, fields=('image'),indent=5, use_natural_keys=True)
        return HttpResponse(output, content_type="application/json")
  • the file is just for debug purposes and it seems to write the correct data
  • also tried return HttpResponse({}, content_type="application/json")
  • 该文件仅用于调试目的,它似乎写入了正确的数据
  • 也试过 return HttpResponse({}, content_type="application/json")

in my application the post request is done using AFNetworking like this:

在我的应用程序中,发布请求是使用 AFNetworking 完成的,如下所示:

-(void) saveImageToDB
{
    NSString* BaseURLString = POST_IMAGE;

    NSData* data = UIImagePNGRepresentation(self.image);


    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    [manager POST:BaseURLString
       parameters:@{@"image":[data base64EncodedString]}
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"image saved successfuly");
         }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Error saving image: %@", error)

         }];
}

so I added this line of code

所以我添加了这行代码

manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];

and got this error: Invalid value around character 1

并收到此错误: Invalid value around character 1

I also try to do:

我也尝试这样做:

AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];

[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];

manager.requestSerializer = requestSerializer;

which ended up always with The request timed out.

结果总是 The request timed out.

please advice what is wrong with my request

请建议我的请求有什么问题

回答by lanzz

You're returning the string Post receivedas a response to POST requests. This string is not valid JSON (if you wanted to return just a string in your JSON response, the correct JSON representation would be "Post received", with quotes), and your JSON deserializer seems to complain about exactly that. Try serializing your response in both branches of your logic.

您正在返回字符串Post received作为对 POST 请求的响应。此字符串不是有效的 JSON(如果您只想在 JSON 响应中返回一个字符串,则正确的 JSON 表示应该是"Post received"带引号的 ),而您的 JSON 反序列化器似乎正是在抱怨这一点。尝试在逻辑的两个分支中序列化您的响应。