Javascript 使用JSON xmlHttprequest从web服务器读取数据库并显示数据?
我不熟悉JSON和phonegap,我正在尝试开发一个应用程序来显示来自web服务器的数据,但我不能在移动设备的web视图中显示数据,我不知道我犯了什么错误。以下是我的主要html代码:Javascript 使用JSON xmlHttprequest从web服务器读取数据库并显示数据?,javascript,json,xmlhttprequest,cordova-plugins,Javascript,Json,Xmlhttprequest,Cordova Plugins,我不熟悉JSON和phonegap,我正在尝试开发一个应用程序来显示来自web服务器的数据,但我不能在移动设备的web视图中显示数据,我不知道我犯了什么错误。以下是我的主要html代码: <html> <head> <style> h1 { border-bottom: 3px solid #cc9900; color: #9
<html>
<head>
<style>
h1 {
border-bottom: 3px solid #cc9900;
color: #996600;
font-size: 30px;
}
table, th , td {
border: 1px solid grey;
border-collapse: collapse;
padding: 5px;
}
table tr:nth-child(odd) {
background-color: #f1f1f1;
}
table tr:nth-child(even) {
background-color: #0055cc;
}
</style>
</head>
<body>
<div id="id01"></div>
<script>
var xmlhttp = new XMLHttpRequest();
var url = "http://databaseofalrais.site90.net/";
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
myFunction(xmlhttp.responseText);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
function myFunction(response) {
var arr = JSON.parse(response);
var i;
var out = "<table>";
for (i = 0; i < arr.length; i++) {
out += "<tr><td>" +
arr[i].Name +
"</td><td>" +
arr[i].Street +
"</td><td>" +
arr[i].Phone +
"</td><td>" +
arr[i].Pincode +
"</td><td>" +
arr[i].Water +
"</td><td>" +
arr[i].Elec +
"</td></tr>";
}
out += "</table>"
document.getElementById("id01").innerHTML = out;
}
</script>
</body>
</html>
如果您需要在移动设备中使用数据,您必须尝试json数组或xml。尝试
json\u encode($return)
转换数组对象中的输出
我给你举个例子,试着用在:
$result = mysqli_query($con, "SELECT * FROM Propertylist'");
while ($row = mysqli_fetch_array($result)) {
$return1['Area'] = $row['CompanyName'];
$return['result'][] = $return1;
}
$return['error'] = 'Yes';
echo json_encode($return);
exit;
通过使用您的代码,我得到了与以前相同的输出。我的问题是我不能在网页上显示数据。我的意思是,在html中,数据不通过json解析。您需要使用json_encode来解析值,因此首先在json数组中添加所有值,然后将数组对象放入json_encode()。就这样。
$result = mysqli_query($con, "SELECT * FROM Propertylist'");
while ($row = mysqli_fetch_array($result)) {
$return1['Area'] = $row['CompanyName'];
$return['result'][] = $return1;
}
$return['error'] = 'Yes';
echo json_encode($return);
exit;