最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Javascript JSON.parse responseText "Unexpected end of input" - Stack Overflow

programmeradmin3浏览0评论

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"]}

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"]}
Share Improve this question edited Dec 7, 2012 at 12:45 dman asked Dec 7, 2012 at 12:35 dmandman 11.1k25 gold badges116 silver badges217 bronze badges 5
  • 3 What does your json look like? – MortenSickel 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 – Leandro Barreto Commented Dec 7, 2012 at 12:37
  • stackoverflow.com/questions/3594923/… – user1703809 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. – pimvdb Commented Dec 7, 2012 at 12:42
  • 2 You'll need to check for the readystate and status – Bergi Commented Dec 7, 2012 at 13:03
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论