簡體   English   中英

在http響應中將圖像發送到ajax響應中,獲取錯誤imagefilefield不可序列化

[英]send image in http response to ajax response getting error imagefilefield is not JSON serializable

我想將圖像從視圖發送到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成功的形象?

阿賈克斯的成功是

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>');
                                                    }
                                },  

請為此幫我,、、、、、、、、、、

圖像對象不是json可序列化的,這就是問題所在,但是如果您只是通過AJAX發送圖像url插入了圖像對象,我認為問題將得到解決。 嘗試在您的view.py中,將以下內容:“ postimage”:postimage更改為以下內容:“” postimage“:postimage.url

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM