Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript JSON.parse responseText "Unexpected end of input"

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"]}
like image 855
dman Avatar asked Dec 07 '12 12:12

dman


1 Answers

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.

like image 81
dman Avatar answered Oct 05 '22 15:10

dman