我想从视图中将图像发送给ajax响应。
view.py
def Display_post(request):
if request.method=="GET":
post = Post.objects.all()
print post
data = []
for pos in post:
postdata = pos.post
userdata = pos.user.username
postimage = pos.image
print type(postimage)
record = {"postdata":postdata, "userdata":userdata, "postimage":postimage}
data.append(record)
response = json.dumps(data,cls=DjangoJSONEncoder)
return HttpResponse(response, mimetype="application/json")
我要进入ajax错误功能。如何在ajax成功获得图像?
ajax的成功是
success:function(response){
alert(response[0].userdata);
len = response.length;
alert(len);
for(i=0; i<=len;i++)
{
post = response[i].postdata;
user = response[i].userdata;
image = response[i].postimage;
$(".posts").append('<div class="col-md-4"><img src="'+image+'" width="100" height="100" /></div>div class="col-md-2">'+user+'</div><div class="col-md-6">'+post+'</div>');
}
},
请帮我这个,,,,,,,,,,,
答案 0 :(得分:2)
图像对象不是json可序列化的,这就是问题所在,但是如果您只是通过AJAX发送图像对象的图像对象,我认为问题将会得到解决。 尝试在你的view.py中改变这个:“postimage”:postimage to this:“postimage”:postimage.url