ajax when trying to access d[obj].src it return undefined

295
November 21, 2016, at 8:15 PM

AJAX

  function ajax_json_gallery(folder) {
      alert(folder);
      var thumbnailbox = $('#thumbnailbox');
      $.ajax({
          type: "POST",
          url: "json_gallery_data.php",
          contentType: "application/x-www-form-urlencoded",
          dataType: "json",
          data: "folder=" + folder,
          success: function(d) {
              for (var obj in d) {
                  if (d.hasOwnProperty(obj)) {
                      alert(d[obj]); //access data// 
                      alert(d[obj].src); //undefined//
                  }
              }
          }
      });
  }

PHP

header('Content-Type: application/json');
$folder = "Img/Company1/Jersey1";
$dir = $folder."/";
$dirHandle = opendir($dir); 
$i = 0;
$directoryfiles = array();
while ($file = readdir($dirHandle)) {
    if(!is_dir($file) && preg_match("/.jpg|.gif|.png/i", $file)){
        $i++;
        $src = "$dir$file";
$directoryfiles[] = '"img'.$i.'":{ "num":"'.$i.'","src":"'.$src.'", "name":"'.$file.'" },';
    }
}
closedir($dirHandle);
echo json_encode($directoryfiles);

console.log(d)

[""img1":{ "num":"1","src":"Img/House1/Type1/Image1.png", "name":"Image1.png" },", 
    ""img2":{ "num":"2","src":"Img/House1/Type1/Image2.png", "name":"Image2.png" },", 
    ""img3":{ "num":"3","src":"Img/House1/Type1/Image3.png", "name":"Image3.png" },", 
    ""img4":{ "num":"4","src":"Img/House1/Type1/Image4.png", "name":"Image4.png" },"]

x3

i am using ajax to get all image inside the folder directory , and return to ajax but when i tried to access the d[o].src it return undefined ,i had no idea what am i missing here.

Answer 1

Don't try to write JSON text yourself. Just create an associative array or stdClass object, add the appropriate key/values, then add that to $directoryfiles. json_encode will then do the proper encoding

$directoryfiles = array();
while ($file = readdir($dirHandle)) {
    if(!is_dir($file) && preg_match("/.jpg|.gif|.png/i", $file)){
        $i++;
        $src = "$dir$file";
        $temp = new stdClass;
        $temp->num = $i;
        $temp->src = $src;
        $temp->name = $file;
        $directoryfiles["img".$i] = $temp;
    }
}
closedir($dirHandle);
echo json_encode($directoryfile);
Answer 2

change the format of json to,

{"img1":{ "num":"1","src":"Img/House1/Type1/Image1.png", "name":"Image1.png" }, 
 "img2":{ "num":"2","src":"Img/House1/Type1/Image2.png", "name":"Image2.png" }, 
 "img3":{ "num":"3","src":"Img/House1/Type1/Image3.png", "name":"Image3.png" }, 
 "img4":{ "num":"4","src":"Img/House1/Type1/Image4.png", "name":"Image4.png" }};

then u use the code,

console.log(d.img1.src);
Rent Charter Buses Company
READ ALSO
Collapse side panel

Collapse side panel

How do I collapse side panel when viewed on smaller screens, so that "panel-header" becomes a toggle button. I.

340
Create an associative array in jquery using php array

Create an associative array in jquery using php array

I need to create an assosiative array in jQuery from PHP. .

393
How to hide search on single select chosen jquery?

How to hide search on single select chosen jquery?

I've read in chosen jquery at http://harvesthq. github.

423
Is there a way to record user's activity offline and update them when they online? [closed]

Is there a way to record user's activity offline and update them when they online? [closed]

I just want to allow users make modification offline with insecure data. Namely modifying their profile name, add new content (page), photos and so forth.

218