如何修复 Jodit 上传器中 JSON 数据错误第 1 行第 1 列的意外字符

问题描述

我正在努力让上传器在 Jodit 中工作。我按照文档/教程和几篇文章中给出的示例进行操作,但我总是收到错误消息“未捕获的语法错误: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."
        );
        
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)