图片未在 Phone Gap 应用中显示
Images not displaying in Phone Gap app
我对 Phone Gap 还很陌生。我对HTML和jQuery有一个清晰的认识。在HTML中看起来很简单的东西在Phone Gap.
中使用并不那么简单。
目前我正在 Android 设备上测试它。
我正在尝试显示图像,图像正在从服务器中提取。
输出HTML
<h3>The Bass Angler</h3>
<table>
<tr>
<td><strong>Price: </strong>R35</td>
</tr>
<tr>
<td>
<img style="width: 50%; height: 50%" src="http://www.adventurebuddy.co.za/uploads/cover.PNG" />
</td>
</tr>
</table>
jQuery
function ViewMag(id)
{
db.transaction(function(tx) {
tx.executeSql("select magid,name,price from mag where magid = " + id + " order by name", [],
function(tx, rs){
html = '<br /><h3>' + rs.rows.item(0).name + '</h3>';
html += '<table><tr><td><strong>Price: </strong>R' + rs.rows.item(0).price + '</td></tr>'
// Load editions for a magazine
db.transaction(function(tx1){
tx1.executeSql("select editionid,editiondate,filepath,cover from editions where deleted is null and magid = " + id, [],
function(tx1,rs1){
if(rs1.rows.length == 0)
html += '<p>No editions found.</p>';
else
{
//html += '<ul class="">';
for(ri1=0;ri1<rs1.rows.length;ri1++)
{
var coverurl = APPSERVER + '/uploads/' + rs1.rows.item(ri1).cover;
var file = APPSERVER + '/uploads/' + rs1.rows.item(ri1).filepath
html += '<tr><td><img style="width: 50%; height: 50%" src="' + coverurl + '" /></td></tr>'
}
html += '</table>';
}
});
});
$.ui.updatePanel('#main',html);
$.ui.setTitle(rs.rows.item(0).name);
$.ui.loadContent('#main',false,false,"right");
},
window.onDBError
);
});
如果我将 HTML 代码保存为 htm 文件,它可以正常工作,但不适用于 phone。我错过了什么?
非常感谢
您需要将域列入白名单。阅读 here.
也就是说你需要添加
<access origin="http://www.adventurebuddy.co.za" />
到 config.xml
文件,以告诉 PhoneGap 这不是木马试图访问随机 URLs
的红色信号
EDIT: for the OP to use a 'hack'.
现在,您可以为 img
元素分配一个 id
。
<img style="width: 50%; height: 50%" id="image" />
然后,根据您的 JavaScript(使用 jQuery),您可以执行以下操作:
$.ajax({
url: 'http://www.adventurebuddy.co.za/uploads/cover.PNG',
type: "GET",
success: function( data ) {
var f = $( "#image" ).attr( "src", data );
console.log(f); // just to check it.
},
error: function( xhr, status, thrownErorr ) {
console.log( xhr + "\r\n" + status + "\r\n" + thrownError );
}
});
如果这不起作用,则意味着您的 phone 阻止 向 URL 发出请求,您需要进入 设备日志.
我找到了解决问题的方法。
变量 html 超出函数 db.transaction() 的范围。我不得不重组我的函数并最终将一些代码移出到一个新函数中
jQuery
function ViewEditions(id){
$.ui.loadContent('#editions',false,false,"right");
db.transaction(function(tx){
tx.executeSql("select editionid,editiondate,filepath,cover from editions where deleted is null and magid = " + id, [],
function(tx,rs){
if(rs.rows.length == 0)
html = '<p>No editions found.</p>';
else {
html = '<ul class="list">';
for(ri=0;ri<rs.rows.length;ri++)
{
var coverurl = APPSERVER + '/uploads/' + rs.rows.item(ri).cover;
var file = APPSERVER + '/uploads/' + rs.rows.item(ri).filepath
html += '<li><a href="#" onclick="downloadFile(' + file + ',' + rs.rows.item(ri).filepath + ');"><img style="width: 50%; height: 50%" src="' + coverurl + '" /><div align="left">' + rs.rows.item(ri).editiondate + '</div></a></li>';
}
html += '</ul>';
}
$('#editionsresults').html(html);
},window.onDBError
);
});
};
我对 Phone Gap 还很陌生。我对HTML和jQuery有一个清晰的认识。在HTML中看起来很简单的东西在Phone Gap.
中使用并不那么简单。目前我正在 Android 设备上测试它。
我正在尝试显示图像,图像正在从服务器中提取。
输出HTML
<h3>The Bass Angler</h3>
<table>
<tr>
<td><strong>Price: </strong>R35</td>
</tr>
<tr>
<td>
<img style="width: 50%; height: 50%" src="http://www.adventurebuddy.co.za/uploads/cover.PNG" />
</td>
</tr>
</table>
jQuery
function ViewMag(id)
{
db.transaction(function(tx) {
tx.executeSql("select magid,name,price from mag where magid = " + id + " order by name", [],
function(tx, rs){
html = '<br /><h3>' + rs.rows.item(0).name + '</h3>';
html += '<table><tr><td><strong>Price: </strong>R' + rs.rows.item(0).price + '</td></tr>'
// Load editions for a magazine
db.transaction(function(tx1){
tx1.executeSql("select editionid,editiondate,filepath,cover from editions where deleted is null and magid = " + id, [],
function(tx1,rs1){
if(rs1.rows.length == 0)
html += '<p>No editions found.</p>';
else
{
//html += '<ul class="">';
for(ri1=0;ri1<rs1.rows.length;ri1++)
{
var coverurl = APPSERVER + '/uploads/' + rs1.rows.item(ri1).cover;
var file = APPSERVER + '/uploads/' + rs1.rows.item(ri1).filepath
html += '<tr><td><img style="width: 50%; height: 50%" src="' + coverurl + '" /></td></tr>'
}
html += '</table>';
}
});
});
$.ui.updatePanel('#main',html);
$.ui.setTitle(rs.rows.item(0).name);
$.ui.loadContent('#main',false,false,"right");
},
window.onDBError
);
});
如果我将 HTML 代码保存为 htm 文件,它可以正常工作,但不适用于 phone。我错过了什么?
非常感谢
您需要将域列入白名单。阅读 here.
也就是说你需要添加
<access origin="http://www.adventurebuddy.co.za" />
到 config.xml
文件,以告诉 PhoneGap 这不是木马试图访问随机 URLs
EDIT: for the OP to use a 'hack'.
现在,您可以为 img
元素分配一个 id
。
<img style="width: 50%; height: 50%" id="image" />
然后,根据您的 JavaScript(使用 jQuery),您可以执行以下操作:
$.ajax({
url: 'http://www.adventurebuddy.co.za/uploads/cover.PNG',
type: "GET",
success: function( data ) {
var f = $( "#image" ).attr( "src", data );
console.log(f); // just to check it.
},
error: function( xhr, status, thrownErorr ) {
console.log( xhr + "\r\n" + status + "\r\n" + thrownError );
}
});
如果这不起作用,则意味着您的 phone 阻止 向 URL 发出请求,您需要进入 设备日志.
我找到了解决问题的方法。
变量 html 超出函数 db.transaction() 的范围。我不得不重组我的函数并最终将一些代码移出到一个新函数中
jQuery
function ViewEditions(id){
$.ui.loadContent('#editions',false,false,"right");
db.transaction(function(tx){
tx.executeSql("select editionid,editiondate,filepath,cover from editions where deleted is null and magid = " + id, [],
function(tx,rs){
if(rs.rows.length == 0)
html = '<p>No editions found.</p>';
else {
html = '<ul class="list">';
for(ri=0;ri<rs.rows.length;ri++)
{
var coverurl = APPSERVER + '/uploads/' + rs.rows.item(ri).cover;
var file = APPSERVER + '/uploads/' + rs.rows.item(ri).filepath
html += '<li><a href="#" onclick="downloadFile(' + file + ',' + rs.rows.item(ri).filepath + ');"><img style="width: 50%; height: 50%" src="' + coverurl + '" /><div align="left">' + rs.rows.item(ri).editiondate + '</div></a></li>';
}
html += '</ul>';
}
$('#editionsresults').html(html);
},window.onDBError
);
});
};