Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I read example in http://www.w3schools.com/json/json_example.asp[^]

And I try it with my mysql database and my PHP code. But it does not work. I am a beginner and I don't know what it wrong. Please help me, thanks. Here is my PHP link: http://xebus2014.tk/demo.php[^]
And I change w3school code like this:
HTML
<!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>
Posted
Updated 7-Jun-15 22:44pm
v2
Comments
F-ES Sitecore 8-Jun-15 4:55am    
The url looks like json in the browser, but it is returning a whole html page along with html\body tags etc. You need to change your php page so that it returns just the json data and nothing else.
Kornfeld Eliyahu Peter 8-Jun-15 5:39am    
No. It's pure JSON...
(you probably checked with some debugging tool and saw the wrapping of the browser...)
Member 10390715 8-Jun-15 6:05am    
Thanks for your help, I forget "header('Content-type: application/json');" in my PHP code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900