简体   繁体   English

无法使用ajax从Django后端将图像获取到phonegap android应用程序

[英]not able to get the image from django backend to phonegap android application using ajax

I am building the android application using the phonegap. 我正在使用phonegap构建android应用程序。 I am using django backend for android application. 我正在将django后端用于android应用程序。

Here i am trying to get the all details of book model, but i am not getting the image in front end but remaining all data for that model i am getting. 在这里,我试图获取书籍模型的所有详细信息,但我没有在前端获得图像,而是保留了所获得的该模型的所有数据。

i am using 我在用

html = render_to_string('crave/book/book.html',{"books":books})
return HttpResponse(html,mimetype="application/json")

to get the data to front end using ajax. 使用ajax将数据传递到前端。

here my book.html file 这是我的book.html文件

{% for book in books  %}
<img style="padding:1%;float:left;" src="/media/{{book.image}}" height="195" width="140"/>
         </div>
         <div class="span8" style="background-color:black; ">
         <em style="color:red;font-family: 'Ubuntu', sans-serif;font-size:1em;">{{book.title}}</em><p style="font-family: 'Lato', sans-serif; font-size:0.7em;">{{ book.discription }}
         <link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet" type="text/css">
        <div id="f" style="padding-top: 3%;padding-bottom: 1%; background-color:black;">
        <div class="container" align="left" style="background-color:black; ">
            <div class="col-lg-4 " style="font-size:0.9em;font-family: 'Ubuntu', sans-serif;">
            add button
            <a href="#" align="right" style="color:red; font-size:0.9em;font-family: 'Ubuntu', sans-serif; float:right;" id="repcrave" class="cli">By {{ book.author }}</a></div>
            </div>
</div>
{% endfor %}

here i am getting all the details like title, author, discription in phonegap application through ajax but only image i am not able to get. 在这里,我通过ajax获得了phonegap应用程序中的标题,作者,说明等所有详细信息,但只有图像我无法获得。 I checked image is saved in the path specified above 我检查图像是否保存在上面指定的路径中

when i do this in browser in django application 当我在Django应用程序的浏览器中执行此操作时

return render(request, "crave/book/book.html", {'books':books})

i am able to get the image for django application. 我能够为Django应用程序获取图像。

here is my ajax to get the html in android application. 这是我的ajax来获取Android应用程序中的html。

$(function () 
         {  
                var domain = localStorage['domain'];
                $.ajax({
                    url: domain + "/book/books/",
                    type: "GET",
                    dataType: 'html',
                    success: function (html) {

                                $('#speicificpost_reply').html(html);
                            },
                        error: function (response) {
                        alert('You dont have your books here');

                        }
                });
        });

i dont know what wrong i am doing here. 我不知道我在这里做什么错。 If any one gone through before please help me out. 如果有人通过之前,请帮助我。

基本上模拟器不会在模拟器浏览器中识别“ localhost”。因此我们必须使用系统的IP地址。在此问题中,只需在src属性中使用http://192.168.0.1/media/{{book.image}} 。 192.168.0.1是我的系统IP地址。

In django itself you can get the image by specifying the media path over there. 在django本身中,您可以通过在那儿指定媒体路径来获取图像。 like /media/{{image}} 像/ media / {{image}}

but for phone gap you need to specify the full path including domain also. 但是对于电话间隔,您需要指定完整路径,包括域。 While you are getting the image in to the phone gap application it need to be have the full path of that image because the is no media_url setting available like its in django settings file. 当您将图像放入电话间隙应用程序时,它必须具有该图像的完整路径,因为没有django设置文件中的media_url设置可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM