dongweihuan8610 2016-06-23 14:54
浏览 46

AJAX返回'undefined'尽管我知道变量尝试返回不是空的

I have a simple php code, which retrieves info from a database, and stores it in a multi dimensional array:

<?php
        //Fetches the mediainfo entries to populate the webpage, up to as many are in the DB.
        error_reporting(E_ALL);
        $db = new mysqli("localhost","root","pass", "Media");
        if($db->connect_errno > 0){
            echo "ERROR";
        die('Unable to connect to database [' . $db->connect_error . ']');
            }

            $data_array=array();
            $sql = "SELECT * FROM mediainfo;";
        //  echo $sql;
            $result = mysqli_query($db,$sql);
            if($result && mysqli_num_rows($result) > 0){
                $row=mysqli_fetch_all($result,MYSQLI_ASSOC);
                mysqli_close($db);
                    for($i = 0; $i < count($row);$i++){

                        $data_array[$i][0] = $row[$i]['ID'];
                        $data_array[$i][1] = $row[$i]['IMGPATH'];
                        $data_array[$i][2] = $row[$i]['TITLE'];
                        $data_array[$i][3] = $row[$i]['RATING'];
                        $data_array[$i][4] = $row[$i]['POSITION'];


                    }
                    //echo $data_array[0][0];
                    return $data_array;


            if(!empty($data_array))
            {
                $data_array[0][0] = "ERROR";
            return $data_array;
            }
            else{
                $data_array[0][0] = "ERROR";
                return $data_array;
            }
            }
            else{
                $error[0][0] = "error";
                return $error;

            }


            //$JSONString = json_encode($data_array);
        //  return $data_array;

?>

Ignore the redundant array fillings, that was just me testing values.

The js is:

function loadMedia(){   
    console.log("please work");
      $.post("getMedia.php",
{


    success: function(data){
            console.log(data[0][0]);

    }})
    }

I'm just trying to print the first element of the first row, to see if it exists. Currently it simply prints 'undefined'. If anyone could provide any insight it would be greatly appreciated.

I'd just like to stress that I DO know that the array is full when I return it. Thanks

Update So following some quick responses(thanks) I've tried the following:

                if($result && mysqli_num_rows($result) > 0){
                $row=mysqli_fetch_all($result,MYSQLI_ASSOC);
                mysqli_close($db);
                    $JSONString = json_encode($row);
                    return $JSONString;

and:

<script>
    function loadMedia(){   
    console.log("please work");
      $.post("getMedia.php",
{
    success: function(data){
            console.log(data);
    }})
    }
</script>

Unfortunately it has the same result.

Second update So I've added header('Content-type: application/json.');, and upon explicitly calling the php file with a button, a download is launched for the JSON string. Progress! The php won't run automatically from my AJAX call though. Or rather, it still returns undefined.

  • 写回答

1条回答 默认 最新

  • duanqianmou4661 2016-06-23 16:29
    关注

    I fixed it. I simply changed from using $.post, to $.ajax format, and it worked. Very frustrating, and I don't have the understanding of why that worked. If you're a fellow newbie running into a similar problem, here's one more solution to try! Thanks for all the responses guys.

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么