7

I keep getting "Uncaught SyntaxError: Unexpected end of input " in the browser console when running this JSON.parse. I tried shortening the $element to just $entry but still no success:

function getFileList() {
        var elements;   
    var foo = document.getElementById("filelist");
    var xhr = new XMLHttpRequest();
    xhr.addEventListener("load", complete, true);

         // foo.innerHTML= xhr.responseText;
    xhr.onreadystatechange = function () {
        console.log(JSON.parse(xhr.responseText));

    }


    xhr.open("GET", "php/dir_list.php", true);
    xhr.send(null);

    function complete() {
        eventAssign();
        xhr.removeEventListener("load", complete, false);
    }

}



<?php
$files = array();
if ($handle = opendir('/trunk')) {
    $i = 0;
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != "..") {
            $element = '<li><a class="files" id="listFile'.$i.'" data-filename="'.$entry.'" href=php/download.php?filename='.$entry.'>'.$entry.'</a></li>';
            $checksum = shell_exec("sum /trunk/".$entry);
            $files[$entry] = array( $element, $checksum );
            $i++;
        }
    }
    closedir($handle);
}
$files =  json_encode($files);
echo $files;
?>

EDIT: Fixed getFileList() pasting.

Just to avoid confusion, issue still persists. Here is the json output from php dir_list.php:

webserver httpfiles # php php/dir_list.php

{"lynx2.8.5rel.1-DOSc.zip":["<li><a class=\"files\" id=\"listFile0\" data-filename=\"lynx2.8.5rel.1-DOSc.zip\" href=php\/download.php?filename=lynx2.8.5rel.1-DOSc.zip>lynx2.8.5rel.1-DOSc.zip<\/a><\/li>","02625  2406\n"],"try.zip":["<li><a class=\"files\" id=\"listFile1\" data-filename=\"try.zip\" href=php\/download.php?filename=try.zip>try.zip<\/a><\/li>","18695    74\n"],"darn.tar.gz":["<li><a class=\"files\" id=\"listFile2\" data-filename=\"darn.tar.gz\" href=php\/download.php?filename=darin.tar.gz>darin.tar.gz<\/a><\/li>","56880   292\n"],"songs.txt":["<li><a class=\"files\" id=\"listFile3\" data-filename=\"songs.txt\" href=php\/download.php?filename=songs.txt>songs.txt<\/a><\/li>","43469     1\n"],"CentOS-6.3-x86_64-minimal-EFI.iso":["<li><a class=\"files\" id=\"listFile4\" data-filename=\"CentOS-6.3-x86_64-minimal-EFI.iso\" href=php\/download.php?filename=CentOS-6.3-x86_64-minimal-EFI.iso>CentOS-6.3-x86_64-minimal-EFI.iso<\/a><\/li>","04152 372736\n"]}
5
  • 3
    What does your json look like? Commented Dec 7, 2012 at 12:37
  • 2
    I don't know if you posted only part of the code, but you are missing the closing } from the getFileList function Commented Dec 7, 2012 at 12:37
  • stackoverflow.com/questions/3594923/… Commented Dec 7, 2012 at 12:41
  • @Alex Mihai: In this case, the JSON is produced by PHP and thus most probably valid. The error is the closing } instead. Commented Dec 7, 2012 at 12:42
  • 2
    You'll need to check for the readystate and status Commented Dec 7, 2012 at 13:03

1 Answer 1

21

Found out the issue...I was getting repeat responses in one call... I added: if (xhr.readyState==4 && xhr.status==200) {... and it fixed the issue. Thanks.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.