I use Yii2 and and I need to connect ckedit5, but when uploading photo i get error - 400 (Bad Request)

128 views Asked by At

$(".editor").each(function () { let id = $(this).attr('id'); ClassicEditor .create(document.querySelector('#'+id), {

            toolbar: [ 'heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', 'blockQuote','imageUpload' ],
            ckfinder: {
            // Upload the images to the server using the CKFinder QuickUpload command.
             headers: {
               tokenUrl: '{$csrf_token}'
            },
                uploadUrl: '/admin/settings/ajax-upload-photo',
            },
            heading: {
                options: [
                    { model: 'paragraph', title: 'Paragraph', class: 'ck-heading_paragraph' },
                    { model: 'heading1', view: 'h1', title: 'Heading 1', class: 'ck-heading_heading1' },
                    { model: 'heading2', view: 'h2', title: 'Heading 2', class: 'ck-heading_heading2' }
                ]
            },
        })
        .then(function (editor) {
            console.log(editor);
        })
        .catch(function (error) {
            console.error(error);
        });
});
1

There are 1 answers

0
Vitaly Litvinov On

please use csrf() in your html form

<form>
<?= Html::hiddenInput(Yii::$app->request->csrfParam, Yii::$app->request->csrfToken); >
</form>

or

$(".editor").attr({
    type: 'hidden',
    name: '_csrf'
    value: $('meta[name=csrf-token]').attr("content");
}).appendTo('form');