将路径传递给php将返回未定义的结果


Passing path to php returns undefined

我正试图传递一个php脚本的路径,该脚本应该在数组中返回该路径中的所有图像文件,但我一直得到的只是未定义的错误,有人能帮忙吗。

function LoadGallery(dir_path) {
  $.ajax({
    type: 'POST',
    url: "getimages.php",
    traditional: true,
    data:{ path : dir_path},
    type: "json",
    success: function(data){
        alert(data[0]);
    //    $("#image-container").val(data[0]);
    }
  });
}

PHP脚本getimages:

<?php
if(isset($_POST['path'])){
    $dir = $_POST['path'];
    $img = array();
    if (is_dir($dir)) {
       if ($hnd = opendir($dir)) {
           while (false !== ($file = readdir($hnd))) {
               if ($file != "." && $file != "..") {
                    $img[] = $file;
                }
           }
           closedir($hnd);
        }
    }
    return $img;
 }
 ?>

更改php代码以返回json和结果。

<?php
if(isset($_POST['path'])){
    $dir = $_POST['path'];
    $img = array();
    if (is_dir($dir)) {
       if ($hnd = opendir($dir)) {
           while (false !== ($file = readdir($hnd))) {
               if ($file != "." && $file != "..") {
                    $img[] = $file;
                }
           }
           closedir($hnd);
        }
    }
    echo json_encode($img);
 }
 ?>