无法使用ajax从django后端获取到phonegap android应用程序的图像
我正在使用phonegap构建android应用程序。我正在为android应用程序使用django后端无法使用ajax从django后端获取到phonegap android应用程序的图像,android,jquery,ajax,django,cordova,Android,Jquery,Ajax,Django,Cordova,我正在使用phonegap构建android应用程序。我正在为android应用程序使用django后端 $(function () { var domain = localStorage['domain']; $.ajax({ url: domain + "/book/books/", type: "GET",
$(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');
}
});
});
在这里,我试图获得图书模型的所有细节,但我没有得到前端的图像,而是保留了我得到的模型的所有数据
我正在使用
html = render_to_string('crave/book/book.html',{"books":books})
return HttpResponse(html,mimetype="application/json")
使用ajax将数据发送到前端
这是我的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 %}
我能够获得django应用程序的图像
$(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');
}
});
});
这是我的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');
}
});
});
我不知道我在这里做错了什么。如果以前有人遇到过,请帮我解决。基本上emulator不会在emulator浏览器中识别“localhost”。所以我们必须使用系统的IP地址。在这个问题中,只需使用
http://192.168.0.1/media/{{book.image}
在src属性中。这里192.168.0.1是我的系统IP地址。在django本身中,您可以通过在那里指定媒体路径来获取映像。像/media/{{image}
但是对于phone gap,您还需要指定包含域的完整路径。当您将图像放入phone gap应用程序时,它需要具有该图像的完整路径,因为没有像django设置文件中那样的媒体url设置。嗨,罗摩克里希纳,最好的方法是使用
{{book.image.url}
而不是http://192.168.0.1/media/{{book.image}
,然后在settings.MEDIA\u URL中配置http://192.168.0.1/media/
。这样,通过更改设置,您将在所有模板中获得新的url。