Answer the question
In order to leave comments, you need to log in
How to pass X-CSRF-Token to Request Headers when uploading a photo via CKEDITOR?
Hello.
I organized the upload of photos in Yii2 via CKEDITOR, uploaded the upload.php folder to the /web directory, and actually indicated
filebrowserImageUploadUrl: '/web/upload.php?command=QuickUpload&type=Images',
photos are uploaded to the server. /controllers создал PhotoController.php
in the controllerpublic function actionUpload(){
приписал правило в web.php 'user/photo-upload' => 'photo/upload',
filebrowserImageUploadUrl: '/user/photo-upload?command=QuickUpload&type=Images',
$.ajax
({
url: '/user/photo-upload?command=QuickUpload&type=Images',
method: 'POST',
data: 'Вася',
success: function(data)
{
console.log(data);
},
error: function(error)
{
console.log(error);
}
});
Answer the question
In order to leave comments, you need to log in
In general, for those who are interested, this is the solution to my problem.
Add this script before initializing your CKEDITOR
var csrfParam = '_csrf';
var csrfValue = $('meta[name=csrf-token]').attr("content");
$(document).off('click', '.cke_dialog_tabs a:eq(1)').on('click', '.cke_dialog_tabs a:eq(1)', function () {
console.log('Вася');
var $form = $('.cke_dialog_ui_input_file iframe').contents().find('form');
if (!$form.find('input[name=' + csrfParam + ']').length) {
var csrfTokenInput = $('<input/>').attr({
'type': 'hidden',
'name': csrfParam
}).val(csrfValue);
$form.append(csrfTokenInput);
}
});
cke_dialog_tabs a:eq(1)
- One here because when you click in the visual editor to add a photo, an iframe with tabs opens, in my case "File Upload" is the second tab, so I have eq (1), You can have the 3rd tab, then it will be eq (2).
Specify the CSRF token in the Ajax settings, for example:
var csrfParam = $("meta[name=csrf-param]").attr("content");
var csrfValue = $("meta[name=csrf-token]").attr("content");
$.ajaxSetup({
headers: {csrfParam: csrfValue}
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question