[英]How to read JSON data from a web server running PHP and MySQL?
I read example in http://www.w3schools.com/json/json_example.asp . 我在http://www.w3schools.com/json/json_example.asp中阅读了示例。
And I try it with my MySQL database and my PHP code. 我将其与MySQL数据库和PHP代码一起尝试。 But it does not work.
但这行不通。 I am a beginner and I don't know what it wrong.
我是一个初学者,我不知道这是怎么回事。
Here is my PHP link: http://xebus2014.tk/demo.php 这是我的PHP链接: http : //xebus2014.tk/demo.php
And I change w3school code like this: 我像这样更改w3school代码:
<!DOCTYPE html> <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: #ffffff; } </style> </head> <body> <h1>Customers</h1> <div id="id01"></div> <script> var xmlhttp = new XMLHttpRequest(); var url = "http://xebus2014.tk/demo.php"; 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].STT + "</td><td>" + arr[i].ID + "</td><td>" + arr[i].Name + "</td><td>" + arr[i].Singer + "</td></tr>"; } out += "</table>" document.getElementById("id01").innerHTML = out; } </script> </body> </html>
您是否将响应设置为Json String?
header("Content-Type: application/json; charset=UTF-8");
Your result is already an object, so you don't need to parse it using JSON.parse, just use it as is and pass it through the loop as is. 您的结果已经是一个对象,因此您无需使用JSON.parse对其进行解析,只需按原样使用并将其按原样通过循环即可。
I don't know about the cross domain issue, but to run it, I go directly to your function... see this fiddle for a working loop using your output -- http://jsfiddle.net/fu3g5Loe/ 我不了解跨域问题,但是要运行它,我直接转到您的函数...请参见此小提琴,使用您的输出进行工作循环-http://jsfiddle.net/fu3g5Loe/
OR you may use this in your PHP backend 或者您可以在PHP后端中使用它
echo json_encode('Your result set goes here'); // this will apply the correct JSON representation of your result set before echoing
Working Code 工作守则
<body>
<h1>Customers</h1>
<div id="id01"></div>
<script>
/*
var xmlhttp = new XMLHttpRequest();
var url = "http://xebus2014.tk/demo.php";
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
myFunction(xmlhttp.responseText);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
*/
var params = [{"STT":"1","ID":"123","Name":"Sexy Love","Singer":"T-ara"},{"STT":"2","ID":"456","Name":"Day By Day","Singer":"T-ara"},{"STT":"3","ID":"789","Name":"Cry Cry","Singer":"T-ara"}];
myFunction(params);
function myFunction(response) {
var arr = response;
var i;
var out = "<table>";
for(i = 0; i < response.length; i++) {
out += "<tr><td>" +
arr[i].STT +
"</td><td>" +
arr[i].ID +
"</td><td>" +
arr[i].Name +
"</td><td>" +
arr[i].Singer +
"</td></tr>";
}
out += "</table>"
document.getElementById("id01").innerHTML = out;
}
</script>
</body>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.