0
JSON數據進行解碼,我想用PHP從MySQL訪問某些數據,然後通過一個AJAX請求的JSON字符串發送到Java腳本。但問題是在解析java腳本中的字符串後,我無法使用這些數據。 PHP代碼將類數組轉換爲JSON字符串,然後發送它。請告訴我這樣做的正確方法,如果有的話。無法在JavaScript
PHP代碼是:
<?php
ini_set('error_reporting', E_STRICT); //Suppress warnings. !!Disable only during developmental time
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "crie_test";
//create connection
$conn = new mysqli($servername,$username,$password,$dbname);
if($conn->connect_error){
die("Connection failed".$conn->connect_error);
}
class publications{
public $dept_name,$dept_code,$int_jour, $nat_jour, $inter_nat_conf, $nat_conf, $int_book_chap, $nat_book_chap;
}
//Fetch department codes
$sql = "SELECT * FROM tbl_dept_info";
$res = $conn->query($sql);
$no_of_dept = mysqli_num_rows($res);
$dept_li[$no_of_dept] = new publications(); //create department object array
//Intialize variables
for($i=0;$i<$no_of_dept;$i++){
$dept_li[$i]->int_jour=0;
$dept_li[$i]->nat_jour=0;
$dept_li[$i]->inter_nat_conf=0;
$dept_li[$i]->nat_conf=0;
$dept_li[$i]->int_book_chap=0;
$dept_li[$i]->nat_book_chap=0;
}
if ($res->num_rows > 0) {
$i = 0;
while($row = $res->fetch_assoc()) {
$dept_li[$i]->dept_code = $row["Dept_Code"];
$dept_li[$i]->dept_name = $row["DeptType"];
$i++;
}
}
else{
echo "Unable to fetch department id's!!";
}
//fetch the research table
$sql1 = "SELECT tbl_dept_research.ResearchCode, tbl_dept_research.DeptCode, tbl_research.Publication FROM tbl_dept_research INNER JOIN tbl_research ON tbl_dept_research.ResearchCode=tbl_research.ResearchCode";
$res1 = $conn->query($sql1);
if($res1->num_rows>0){
while($row = $res1->fetch_assoc()) {
$dep_code = $row["DeptCode"];
for($i=0;$i<$no_of_dept;$i++){
if($dept_li[$i]->dept_code==$dep_code){
switch($row['Publication']){
case"International Journal": $dept_li[$i]->int_jour++;
break;
case"National Journal": $dept_li[$i]->nat_jour++;
break;
case"International Conference": $dept_li[$i]->inter_nat_conf++;
break;
case"National Conference": $dept_li[$i]->nat_conf++;
break;
case"Book Chapter International": $dept_li[$i]->int_book_chap++;
break;
case"Book Chapter National": $dept_li[$i]->nat_book_chap++;
break;
}
}
}
}
}
echo json_encode($dept_li);
$conn->close();
?>
客戶端側腳本是:
<!DOCTYPE html>
<html>
<body>
<h2>Get data as JSON from a PHP file on the server.</h2>
<p id="demo"></p>
<script>
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myObj = JSON.parse(this.responseText);
document.getElementById("demo").innerHTML = myObj.0.dept_name;
}
};
xmlhttp.open("GET", "sss.php", true);
xmlhttp.send();
</script>
</body>
</html>
同樣對於參考正在由PHP腳本生成此JSON字符串:
{
"8": {
"dept_name": null,
"dept_code": null,
"int_jour": null,
"nat_jour": null,
"inter_nat_conf": null,
"nat_conf": null,
"int_book_chap": null,
"nat_book_chap": null
},
"0": {
"int_jour": 10,
"nat_jour": 1,
"inter_nat_conf": 16,
"nat_conf": 14,
"int_book_chap": 1,
"nat_book_chap": 4,
"dept_code": "101",
"dept_name": "ECE"
},
"1": {
"int_jour": 22,
"nat_jour": 1,
"inter_nat_conf": 32,
"nat_conf": 16,
"int_book_chap": 5,
"nat_book_chap": 0,
"dept_code": "102",
"dept_name": "CSE"
},
"2": {
"int_jour": 12,
"nat_jour": 4,
"inter_nat_conf": 10,
"nat_conf": 23,
"int_book_chap": 1,
"nat_book_chap": 0,
"dept_code": "103",
"dept_name": "IT"
},
"3": {
"int_jour": 21,
"nat_jour": 0,
"inter_nat_conf": 9,
"nat_conf": 35,
"int_book_chap": 0,
"nat_book_chap": 0,
"dept_code": "104",
"dept_name": "EE"
},
"4": {
"int_jour": 13,
"nat_jour": 1,
"inter_nat_conf": 8,
"nat_conf": 33,
"int_book_chap": 0,
"nat_book_chap": 1,
"dept_code": "105",
"dept_name": "MCA"
},
"5": {
"int_jour": 10,
"nat_jour": 5,
"inter_nat_conf": 12,
"nat_conf": 13,
"int_book_chap": 0,
"nat_book_chap": 1,
"dept_code": "106",
"dept_name": "MBA"
},
"6": {
"int_jour": 57,
"nat_jour": 6,
"inter_nat_conf": 5,
"nat_conf": 10,
"int_book_chap": 0,
"nat_book_chap": 1,
"dept_code": "109",
"dept_name": "AS"
},
"7": {
"int_jour": 0,
"nat_jour": 0,
"inter_nat_conf": 0,
"nat_conf": 0,
"int_book_chap": 0,
"nat_book_chap": 0,
"dept_code": "110",
"dept_name": "CIVIL"
}
}
請侑碼減少相關的部分,並指定什麼是行不通的。 –
使用'F12'在瀏覽器中打開開發conosle和檢查什麼是通過AJAX調用,你得到什麼真正的netwework標籤。 – JustOnUnderMillions