简体   繁体   English

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

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

This question is asked many times and all of them in a different way that's why I doubt my problem was addressed in them. 这个问题被问了很多遍,而且所有问题都以不同的方式提出,这就是为什么我怀疑我的问题在其中得到解决的原因。

I use TinyMCE with a file uploader. 我将TinyMCE与文件上传器一起使用。 If I use an URL it works because it can get it back from the internet. 如果我使用URL,那么它会起作用,因为它可以从Internet取回它。 When I want to use an image in a folder it will upload it's image in TinyMCE with on top the box saying "Image uploading: 100%" . 当我想在文件夹中使用图片时,它将在TinyMCE中上传图片,并在顶部显示“图片上传:100%” But this message stays on top and when I post the content everything but the image will be sent. 但是,此消息始终停留在顶部,当我发布内容时,除了图像之外的所有内容都会发送出去。 Looking back I noticed no content in the file directory where I want to place all the uploaded images. 回顾过去,我没有发现要放置所有上载图像的文件目录中的内容。 Also, it gave back an error. 另外,它还返回了一个错误。

Unexpected token < in JSON at position 0: Looked up at the error and found something funny. JSON中位于位置0的意外令牌<:查找错误并发现了一些有趣的东西。 It showed the path where the content goes. 它显示了内容的去向。

move_uploaded_file(this_is_the_path/this_is_the_image.jpg)

Well in this example it looks fine but in dev tools, the path is indicated as orange which is a string but the file is in black. 在此示例中看起来不错,但在开发工具中,该路径表示为橙色,这是一个字符串,但文件为黑色。 Does here lies the problem? 问题出在这里吗? I wrapped it in a string and obvious result was: 我将其包裹在字符串中,明显的结果是:

move_uploaded_file(this_is_the_path/'this_is_the_image.jpg')

Which is obviously wrong. 这显然是错误的。

2 side problems which are related but aren't important at the moment to count as a problem. 与之相关但目前不重要的2个副问题可以算作一个问题。

If I change certain parts. 如果我更改某些部分。 Not sure which. 不知道哪个。 It goes from error located to VM'random number' to frontpage where the action is happening. 它从错误定位到VM的“随机数”到正在执行操作的首页。

files who start with numbers doesn't get uploaded at all and will be messaged as error: 500. 以数字开头的文件根本不会上传,并且会显示错误消息:500。

After the main problem is solved I will look into that. 解决主要问题后,我会进行调查。

My PHP code for handling the path. 我的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");
   }
?>

Is the pathing wrong or is it a typo or is it something else? 路径错误还是错别字或其他内容?

$(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();
        }

});
});     

Warning: move_uploaded_file(uploaded_content_frontpage/blobid1510920245387.jpg): failed to open stream: No such file or directory in C:\\wamp\\www\\wysiwyg\\tinymce_main\\wysiwyg_one_page_version2.1\\views\\home\\code_for_images\\image_uploader.php on line 45 警告: 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行中没有此类文件或目录

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

As you can see I can't open stream because no such file is in my directory. 如您所见,我无法打开流,因为目录中没有此类文件。

Comment out 注释掉

echo $filetowrite;

and check, because the filepath is being echoed out before your json, and that is what is causing the JSON parsing error. 并检查一下,因为文件路径在json之前被回显,这就是导致JSON解析错误的原因。 Also there is an extra '.' 也有一个额外的“。” in your 在你的

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

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

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