I am working on a project with laravel 9.
I have a problem with a request in axios, I get the following error:
405 Method Not Allowed
I have already applied the following commands in my project:
php artisan cache:clear
php artisan route:clear
php artisan config:clear
npm run build
But the error persists.
This problem only occurs with one specific request, it is with the add record request.
js file
var fileInput = document.getElementById('formFile');
var file = fileInput.files[0];
const formData = new FormData();
// Agregar el archivo al FormData
formData.append('imageFile', file, file.name);
formData.append('nombre', nombre);
formData.append('url', url);
formData.append('fechaPublicacion', fechaPublicacion);
formData.append('horaPublicacion', horaPublicacion);
formData.append('tiempoLectura', tiempoLectura);
formData.append('textoResumen', textoResumen);
formData.append('temaId', temaId);
formData.append('activoPost', activoPost);
formData.append('detalleDataPost', JSON.stringify(detalleDataPost));
formData.append('postRelacionados', JSON.stringify(arrayPostIds));
axios.post(baseUrl+"/registrar-articulo", formData, {
headers: {
'X-CSRF-TOKEN': window.CSRF_TOKEN,
'Content-Type': 'multipart/form-data'
}
})
.then(function(response) {
Controller
public function regitrarArticulo(Request $request){
$msg = "";
$exito = 0;
$nombre = $request->get("nombre");
$url = $request->get("url");
$fechaPUblicacion = $request->get("fechaPublicacion");
$horaPublicación = $request->get("horaPublicacion");
$tiempoLectura = $request->get("tiempoLectura");
$textoResumen = $request->get("textoResumen");
$temaId = $request->get("temaId");
$activoPost = $request->get("activoPost");
$detallePost = $request->get('detalleDataPost');
$postRelacionados = json_decode($request->get('postRelacionados'), true);
$usuarioAgrego = Auth::user()->id;
$fechaFinal = $fechaPUblicacion." ".$horaPublicación.":00";
$imageName = '';
$path = '';
$imageName = time().'.'.$request->imageFile->extension();
$path = $request->file('imageFile')->storeAs('images', $imageName, 'public');
$articulosEncontrado = DB::table('posts')
->join('temas', 'temas.id', 'posts.tema_id')
->where('posts.urlpost', $url)
->where('temas.id', $temaId)
->get();
if(count($articulosEncontrado) > 0){
Web.php
Route::post('/registrar-articulo', [PostController::class, 'regitrarArticulo'])->middleware('auth')->name('registrar-articulo');
Route::post('/modificar-articulo', [PostController::class, 'updatePost'])->middleware('auth')->name('modificar-articulo');
Route list
I think the problem is in how I make the request in axios, because I have to point out that I have another request that works for the modification using the post method, but this one responds correctly.
I need to know what is the error I have, or the consideration I am missing to apply to my project, to make it work correctly.