如何修复jodit uploader中json数据错误第1行第1列的意外字符

pinkon5k  于 2021-09-29  发布在  Java
关注(0)|答案(0)|浏览(165)

我正在努力让上传程序在jodit中工作。我遵循了docs/tutorial中给出的示例和几篇文章,但我总是得到一个错误“uncaughtsyntaxerror:json.parse:json数据第1行第1列的意外字符”。
有人能帮忙吗?
代码如下:

<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="/public/css/jodit.min.css">
<title>Jodit</title>
</head>
<body>

<textarea class="textarea" name="editor" id="editor"></textarea>

<script src="/public/js/jodit.min.js"></script>
<script language="javascript" type="text/javascript">
var uploadOptions = {
    enableDragAndDropFileToEditor: true,
    uploader: {
        url: '/public/fn/upload.php',
        format: 'json', // Response format
        data: {}, // Pass whatever post data you need here
        isSuccess: function(resp) {
            return !resp.error;
        },
        getMsg: function(resp) {
            return resp.msg.join !== undefined ? resp.msg.join(' ') : resp.msg;
        },
        baseurl: '/public/images/blog', // Ex: /subdir/path/to/files/
        process: function(resp) {
            // Use custom response from upload to build expected object on success
            let files = [];
            resp.list.map((file) => {
            files.push(file.name);
            });
            // Passes through to defaultHandlerSuccess as response
            return { 
            files, // {array} The names of uploaded files. Field name uploader.filesVariableName
            path: relativePathURL, // {string} Real relative path 
            baseurl: relativePathURL, // {string} Base url for filebrowser
            error: (resp.success ? 0 : 1), // {int}
            msg: resp.message // {string}
            };
        },
        error: function(e) {
            console.log(e);
        },
        defaultHandlerSuccess: function(resp) {  
            if (resp.files && resp.files.length) {
                for (let i = 0; i < resp.files.length; i++) {
                    let full_file_path = resp.path + resp.files[i];
                    this.selection.insertImage(full_file_path, null, 250);
                }
            }
        },
        defaultHandlerError: function (resp) {
            this.events.fire('errorPopap', [this.options.uploader.getMsg(resp)]);
        }
    }
}

var editor = new Jodit("#editor", uploadOptions);

</script>

</html>

以及用于移动上载文件的php文件:

if(isset($_FILES['files'])){

    // Count # of uploaded files in array
    $total = count($_FILES['files']['name']);

    // Check if any files have been selected
    if ($total > 0) {

        // Default return array
        $return_list = [];

        // Loop through files
        for ($i = 0; $i < $total; $i++) {

            // Notice how the file data has been received
            $file_data = array(
            'name' => $_FILES['files']['name'][$i],
            'tmp_name' => $_FILES['files']['tmp_name'][$i],
            'size' => $_FILES['files']['size'][$i],
            'type' => $_FILES['files']['type'][$i],
            'error' => $_FILES['files']['error'][$i]
            );

            // Use whatever function to upload signle file
            $file_response = handleUploadSingleFile($file_data);

            // If response is true, add to array
            if ($file_response) {
                $return_list[] = $file_response;
            }

        }

        // The return that is received on the Process function
        return array(
        'success' => true,
        'message' => "Upload completed.",
        "list" => $return_list
        );

    } else {

        return array(
        'success' => false,
        'message' => "Please select file."
        );

    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题