2015-06-08 39 views
1

我看了http://www.w3schools.com/json/json_example.asp的例子。如何從運行PHP和MySQL的Web服務器讀取JSON數據?

我試着用我的mysql數據庫和我的PHP代碼。但它不起作用。 我是一個初學者,我不知道它是什麼問題。請幫助我,謝謝。 這是我的PHP鏈接: http://xebus2014.tk/demo.php

我改變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) \t { 
 
    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>
PHP code

+0

也發表您的PHP代碼。不要將php放在代碼片段中(因爲它不起作用) – SuperDJ

+0

什麼是您的錯誤消息 – Gayathri

+0

您不需要解析響應,因爲它已經是JSON。這可能是錯誤。 –

回答

1

你設定的響應作爲JSON字符串?

header("Content-Type: application/json; charset=UTF-8"); 
+0

我在我的PHP代碼中添加了標題。但它仍然不顯示數據。 – nistelrooy41001662

1

你的結果已經是一個對象,所以你不需要使用JSON.parse解析它,只是把它當作是並使之通過循環原樣。

我不知道跨域問題,但運行它,我直接去你的函數...用你的輸出看到這搗鼓一個工作循環 - http://jsfiddle.net/fu3g5Loe/

,或者您可能使用這在你的PHP後臺

echo json_encode('Your result set goes here'); // this will apply the correct JSON representation of your result set before echoing 

工作守則

<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> 
+0

我想從服務器讀取數據庫。控制檯響應我「\t 請避免在評論中進行擴展討論。您想自動將此討論移至聊天嗎?」 @Dexter Huinda – nistelrooy41001662

+0

只是嘗試用'var arr = response'替換'var arr = JSON.parse(response)'的行並再次測試。 –

+0

好的,它的工作原理。非常感謝你。 – nistelrooy41001662

相關問題