无法在javascript中解码JSON数据

时间:2017-03-06 13:41:14

标签: javascript php json html5

我想使用PHP从mysql访问一些数据,然后通过AJAX请求将其作为JSON字符串发送到java脚本。但问题是在java脚本中解析字符串后,我无法使用数据。 PHP代码将类的数组转换为JSON字符串,然后发送它。如果有的话,请告诉我这样做的正确方法。

php代码是:

tr

客户端脚本是:

<?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();
        ?>

此外,作为参考,这个JSON字符串由PHP脚本生成:

<!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>

1 个答案:

答案 0 :(得分:0)

PHP son_encode返回一个数组,然后你需要数组索引(不仅是索引)

 document.getElementById("demo").innerHTML = myObj.[0].dept_name;