我发现这个错误发生在许多其他用户身上,虽然尝试了许多建议的解决方案似乎没有任何效果,所以我试图发布我的具体案例。
我正在尝试使用django将图像从iphone应用程序保存到我的postgresql数据库。
我的观点如下:
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")
在我的应用程序中,post请求是使用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)
}];
}
所以我添加了这行代码
manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
并收到此错误:Invalid value around character 1
我也试着这样做:
AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];
[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
manager.requestSerializer = requestSerializer;
始终以The request timed out.
请告知我的请求有什么问题
答案 0 :(得分:1)
您将返回字符串Post received
作为对POST请求的响应。此字符串无效JSON(如果您只想在JSON响应中返回一个字符串,正确的JSON表示将是"Post received"
,带引号),并且您的JSON反序列化器似乎正在抱怨这一点。尝试在逻辑的两个分支中序列化您的响应。