繁体   English   中英

JSON中的意外令牌<在位置0 tinymce

[英]Unexpected token < in JSON at position 0 tinymce

这个问题被问了很多遍,而且所有问题都以不同的方式提出,这就是为什么我怀疑我的问题在其中得到解决的原因。

我将TinyMCE与文件上传器一起使用。 如果我使用URL,那么它会起作用,因为它可以从Internet取回它。 当我想在文件夹中使用图片时,它将在TinyMCE中上传图片,并在顶部显示“图片上传:100%” 但是,此消息始终停留在顶部,当我发布内容时,除了图像之外的所有内容都会发送出去。 回顾过去,我没有发现要放置所有上载图像的文件目录中的内容。 另外,它还返回了一个错误。

JSON中位于位置0的意外令牌<:查找错误并发现了一些有趣的东西。 它显示了内容的去向。

move_uploaded_file(this_is_the_path/this_is_the_image.jpg)

在此示例中看起来不错,但在开发工具中,该路径表示为橙色,这是一个字符串,但文件为黑色。 问题出在这里吗? 我将其包裹在字符串中,明显的结果是:

move_uploaded_file(this_is_the_path/'this_is_the_image.jpg')

这显然是错误的。

与之相关但目前不重要的2个副问题可以算作一个问题。

如果我更改某些部分。 不知道哪个。 它从错误定位到VM的“随机数”到正在执行操作的首页。

以数字开头的文件根本不会上传,并且会显示错误消息:500。

解决主要问题后,我会进行调查。

我的PHP代码用于处理路径。

<?php
    $accepted_origins = array("http://localhost:8080");
    $imageFolder = "uploaded_content_frontpage/";
    reset ($_FILES);
    $temp = current($_FILES);
    if (is_uploaded_file($temp['tmp_name'])){
        if (isset($_SERVER['HTTP_ORIGIN'])) {
            if (in_array($_SERVER['HTTP_ORIGIN'], $accepted_origins)) {
                header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
            } else {
                header("HTTP/1.0 403 Origin Denied");
                return;
            }
        }

// Sanitize input
        if (preg_match("/([^\w\s\d\-_~,;:\[\]\(\).])|([\.]{2,})/", $temp['name'])) {
            header("HTTP/1.0 500 Invalid file name.");
            return;
        }

// Verify extension
        if (!in_array(strtolower(pathinfo($temp['name'], PATHINFO_EXTENSION)), array("gif", "jpg", "png"))) {
            header("HTTP/1.0 500 Invalid extension.");
            return;
        }

      // Accept upload if there was no origin, or if it is an accepted origin
        $filetowrite = 'uploaded_content_frontpage/'.$temp['name'];
        move_uploaded_file($temp['tmp_name'], $filetowrite);

        echo json_encode(array('location' => $filetowrite));
   } else {
        // Notify editor that the upload failed
        header("HTTP/1.0 500 Server Error");
   }
?>

路径错误还是错别字或其他内容?

$(document).ready(function() {
tinymce.init ({
        theme: 'modern',
        selector: '.add_body_structure',
        height: 1000,
        menubar: true,
        branding: false,
        toolbar: 'undo redo | styleselect bold italic forecolor backcolor fontselect fontsizeselect | link paste | alignleft aligncenter alignright alignjustify | outdent indent bullist numlist | removeformat | insert',
        plugins: ' contextmenu print preview searchreplace autolink directionality visualblocks visualchars fullscreen image link media mediaembed template table charmap hr pagebreak nonbreaking anchor toc insertdatetime advlist lists textcolor wordcount imagetools contextmenu colorpicker textpattern help autoresize noneditable', 
        contextmenu: 'paste link image inserttable cell row column deletetable',
        advlist_bullet_styles: 'square',
        advlist_number_styles: 'lower-alpha,lower-roman,upper-alpha,upper-roman',
        statusbar: false,
        browser_spellcheck: true,
        image_title: true,      
        automatic_uploads: true,
        media_live_embeds: true,
        contextmenu: true,
        relative_urls: false,
        remove_script_host: false,
        paste_data_images: true,
        encoding: 'xml',
        invalid_elements: 'script, input, textarea, textfield, col, colgroup, caption, dir, meta, object, select, option, source, title',
        fontsize_formats: '8pt 10pt 12pt 14pt 16pt 18pt 20pt 22pt 24pt 26pt 28pt 30pt 32pt 34pt 36pt 38pt 40pt',
        images_upload_url: '/wysiwyg/tinymce_main/wysiwyg_one_page_version2.1/views/home/code_for_images/image_uploader.php',

        file_picker_callback: function(cb, value, meta) {
            var input = document.createElement('input');
            input.setAttribute('type', 'file');
            input.setAttribute('accept', 'image/*, audio/*, video/*');

            input.onchange = function() {
                var file = this.files[0];

                var reader = new FileReader();
                reader.onload = function () {

                    var id = 'blobid' + (new Date()).getTime();
                    var blobCache =  tinymce.activeEditor.editorUpload.blobCache;
                    var base64 = reader.result.split(',')[1];
                    var blobInfo = blobCache.create(id, file, base64);
                    blobCache.add(blobInfo);
                    // call the callback and populate the Title field with the file name
                    cb(blobInfo.blobUri(), { title: file.name });
                };
                reader.readAsDataURL(file);
            };
        input.click();
        }

});
});     

警告: move_uploaded_file(uploaded_content_frontpage / blobid1510920245387.jpg):无法打开流:C:\\ wamp \\ www \\ wysiwyg \\ tinymce_main \\ wysiwyg_one_page_version2.1 \\ views \\ home \\ code_for_images \\ image_uploader.php在第行中没有此类文件或目录

第45行指示move_uploaded_file($ temp ['tmp_name'],$ filetowrite);

如您所见,我无法打开流,因为目录中没有此类文件。

注释掉

echo $filetowrite;

并检查一下,因为文件路径在json之前被回显,这就是导致JSON解析错误的原因。 也有一个额外的“。” 在你的

$filetowrite = 'uploaded_content_frontpage/'.$temp['name'].(see here);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM