Bootstrap fileinput插件json错误

时间:2016-04-19 14:50:15

标签: php

点击删除按钮,我收到此错误。 SyntaxError:JSON.parse:JSON数据第1行第1列的意外数据结尾。有人可以帮助我解决它,因为所有解决问题的努力都证明是徒劳的。

这是错误的屏幕截图。

json error

这是我的目录结构的屏幕截图。 directory structure

这是代码

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8"/>
    <title>Bootstrap File Upload</title>

    <link href="http://netdna.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet">
    <link href="css/fileinput.min.css" media="all" rel="stylesheet" type="text/css" />

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="js/fileinput.min.js" type="text/javascript"></script>
    <script src="http://netdna.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js" type="text/javascript"></script>

    </head>

    <body>



    <input id="files" name="images[]" type="file" multiple=true class="file-loading">
    </body>
    <?php   
    $directory = "images_/";      
    $images = glob($directory . "*.*");
    ?>

    <script>
    $("#files").fileinput({
    uploadUrl: "upload.php", 
    uploadAsync: false,
    minFileCount: 1,
    maxFileCount: 20,
    showUpload: false, 
    showRemove: false,
    initialPreview: [
    <?php foreach($images as $image){?>
        "<img src='<?php echo $image; ?>' height='120px' class='file-preview-image'>",
    <?php } ?>],
    initialPreviewConfig: [<?php foreach($images as $image){ $infoImages=explode("/",$image);?>
    {caption: "<?php echo $infoImages[1];?>",  height: "120px", url: "delete.php", key:"<?php echo $infoImages[1];?>"},
    <?php } ?>]
    }).on("filebatchselected", function(event, files) {

    $("#files").fileinput("upload");

    });

    </script>
</html>

这是我的upload.php

<?php 
$enclosedFolder="images_/";
// Count sent by plugin
$Images =count(isset($_FILES['images']['name'])?$_FILES['images']['name']:0);
$infoImagesUploaded = array();
for($i = 0; $i < $Images; $i++) {
    // The name and the temporary file name we're gonna attach
    $nameFile=isset($_FILES['images']['name'][$i])?$_FILES['images']['name'][$i]:null;
    $nameTemp=isset($_FILES['images']['tmp_name'][$i])?$_FILES['images']['tmp_name'][$i]:null;

    $pathFile=$enclosedFolder.$nameFile;

    move_uploaded_file($nameTemp,$pathFile);

    $infoImagesUploaded[$i]=array("caption"=>"$nameFile","height"=>"120px","url"=>"delete.php","key"=>$nameFile);
    $ImagesUploaded[$i]="<img  height='120px'  src='$pathFile' class='file-preview-image'>";
    }
$arr = array("file_id"=>0,"overwriteInitial"=>true,"initialPreviewConfig"=>$infoImagesUploaded,
             "initialPreview"=>$ImagesUploaded);
echo json_encode($arr);
?>

这是我的delete.php

<?php 
$enclosedFolder="images_/";
if($_SERVER['REQUEST_METHOD']=="DELETE"){
            parse_str(file_get_contents("php://input"),$dataDELETE);
            $key= $dataDELETE['key'];
            unlink($enclosedFolder.$key);

            echo 0;
}
?>

2 个答案:

答案 0 :(得分:0)

Github

  

您的服务器代码返回无效的json格式时出错   你的上传回复。用firebug或javascript控制台调试它   决定返回正确的JSON格式。你必须返回一个正确的JSON   来自你的AJAX的回应(即使你没有任何东西......你必须   至少返回一个空的JSON对象,如{})。

答案 1 :(得分:0)

在PHP文件中,我将空JSON反馈给Ajax

返回“ {}”;

回显“ {}”;