Check if you have this endpoint /creer-un-projet defined in your routes file as POST.
Jul 26, 2019
11
Level 2
error 405 method not allowed QuillJs
Hi everyone.
I use the last version of QuillJS and I would to change the base64 of the image to the URL. I discovered this code to do that :
import Quill from 'quill';
var toolbarOptions = [
['bold', 'italic', 'underline'], // toggled buttons
[{
'list': 'ordered'
}, {
'list': 'bullet'
}],
['image'], // text direction
['video'],
['link'],
];
var quill = new Quill('#editor', {
modules: {
toolbar: toolbarOptions,
},
placeholder: 'Détaillez nous au mieux votre projet',
theme: 'snow'
});
/**
* Step1. select local image
*
*/
function selectLocalImage() {
const input = document.createElement('input');
input.setAttribute('type', 'file');
input.click();
// Listen upload local image and save to server
input.onchange = () => {
const file = input.files[0];
// file type is only image.
if (/^image\//.test(file.type)) {
saveToServer(file);
} else {
console.warn('You could only upload images.');
}
};
}
/**
* Step2. save to server
*
* @param {File} file
*/
function saveToServer(file, File) {
const fd = new FormData();
fd.append('image', file);
const xhr = new XMLHttpRequest();
xhr.open('POST', 'http://localhost:3000/creer-un-projet', true);
xhr.onload = () => {
if (xhr.status === 200) {
// this is callback data: url
const url = JSON.parse(xhr.responseText).data;
insertToEditor(url);
}
};
xhr.send(fd);
}
/**
* Step3. insert image url to rich editor.
*
* @param {string} url
*/
function insertToEditor(url, string) {
// push image url to rich editor.
const range = quill.getSelection();
quill.insertEmbed(range.index, 'image', `http://localhost:3000${url}`);
}
// quill editor add image handler
quill.getModule('toolbar').addHandler('image', () => {
selectLocalImage();
});
var form = $('form');
$(form).submit(function (e) {
// Populate hidden form on submit
e.preventDefault();
var about = $('input[name=editor]');
// $(about).value = JSON.stringify(quill.getContents());
$(about).val(JSON.stringify(quill.getContents()));
// JSON.stringify($(about));
console.log("Submitted", $(form).serialize(), $(form).serializeArray());
// No back end to actually submit to!
return true;
});
but I have this error
POST http://localhost:3000/creer-un-projet 405 (Method Not Allowed) saveToServer @ app.js:49369 input.onchange @ app.js:49342
Can U help me to fix this error please ?
Thank you by advance !
Please or to participate in this conversation.