我想从远程数据库中检索信息并将其显示在列表中。什么是最好的方法?目前我正在调用一个PHP但无法使其工作。有什么建议吗?提前致谢
---- ----的index.html
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, minimum-scale=1, maximum-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Index</title>
<link href="css/custom.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0/jquery.mobile- 1.0.min.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.js"></script>
<script src="cordova-2.6.0.js"></script>
<script>
$(document).ready(function() {
$("#resultBlock").html("Waiting for data...");
$.get("http://localhost/select.php", {}, function(data) {
$("#resultBlock").html(data);
})
});
</script>
</head>
<body>
<div data-role="page" id="inicio">
<div data-role="header" data-position="fixed">
<h1>Periódico</h1>
</div>
<div data-role="content">
<p id="resultBlock"></p>
</div>
<div data-role="footer" data-id="foo1" data-position="fixed" >
<div data-role="navbar">
<ul>
<li><a href="" class="ui-btn-active ui-state-persist"><img src="images/periodico-icon.png" width="40" height="40"><br/>Periodico</a></li>
<li><a href="#directorio" ><img src="images/directorio-icon.png" width="40" height="40"><br/>Directorio</a></li>
<li><a href="#clasificados" ><img src="images/clasificados-icon.png" width="40" height="40"><br/>Clasificados</a></li>
<li><a href="#cuponera" ><img src="images/cuponera-icon.png" width="40" height="40"><br/>Cuponera</a></li>
</ul>
</div>
</div>
</div>
</body>
</html>
- - - - - - - - select.php
<?php
$con=mysqli_connect("localhost","localhost","","test");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<table border='0'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
答案 0 :(得分:0)
我记得在使用phonegap进行开发时遇到了jquery ajax请求的问题。尝试使用经典的ajax请求,它对我有用。另一个可能的问题是您正在请求计算机上的本地文件。示例代码如下:
function loadContent(){
var xmlhttp = getXmlHttpObject();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("listContent").innerHTML=xmlhttp.responseText;
}
};
var url = "http://example.com/select.php";
xmlhttp.open("GET",url,true);
xmlhttp.send();
获取xmlhttp对象:
function getXmlHttpObject(){
var xmlhttp;
if (window.XMLHttpRequest)
xmlhttp=new XMLHttpRequest();
else
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
return xmlhttp;
}