Tek bir formdan add ve update işlemi yapmaya çalışıyorum. Post istediği sorunsuz çalışıyor ancak update fonksiyonu çalışmıyor. Put isteği için 405 (Method Not Allowed) hatası alıyorum.
<form @submit.prevent="saveEvent">
saveEvent() {
if (!this.params.start || !this.params.end) {
this.showMessage('Başlangıç ve Bitiş tarihleri zorunludur.', 'error');
return;
}
let formData = new FormData();
formData.append('title', this.params.title);
formData.append('start', new Date(this.params.start).toISOString());
formData.append('end', new Date(this.params.end).toISOString());
formData.append('description', this.params.description);
const routeUrl = "{{ url('/api/events') }}";
const csrfToken = "{{ csrf_token() }}";
let url = this.params.id ? `${routeUrl}/${this.params.id}` : routeUrl;
let method = this.params.id ? 'PUT' : 'POST';
fetch(url, {
method: method,
body: formData,
headers: {
'X-CSRF-TOKEN': csrfToken
},
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
if (this.params.id) {
// Update event in the calendar
let eventIndex = this.events.findIndex(event => event.id === this.params.id);
if (eventIndex !== -1) {
this.events[eventIndex].title = data.title;
this.events[eventIndex].start = data.start;
this.events[eventIndex].end = data.end;
this.events[eventIndex].description = data.description;
}
} else {
// Add new event to the calendar
this.events.push({
id: data.id,
title: data.title,
start: data.start,
end: data.end,
description: data.description,
});
}
this.calendar.refetchEvents();
this.showMessage('Events başarıyla ' + (this.params.id ? 'güncellendi' : 'eklendi') + '.', 'success');
this.isAddEventModal = false;
})
.catch(error => {
this.showMessage('Events ' + (this.params.id ? 'güncellenirken' : 'eklenirken') + ' bir hata oluştu. Lütfen tekrar deneyin.', 'error');
console.error('There was a problem with the fetch operation:', error);
});
},