undefined usually means you have made a type-o in you code.
Probably here
e.target.dataset.url
Hello, I don't know If this has been solved before, but I'm searching here on laracasts forum and other websites and I can't find anything... but my issue is that I'm performing some asynchronous requests with javascript fetch and, sometimes, It will respond with a 404 status with the url as something like this:
127.0.0.1/undefined
I wanna add that just after I've made the changes to the database I reload the page with the location.reload() javascript funcion (I've even set the function argument as true and still nothing). I'll attach one of my controller's code and the javascript request
This would be my controller with the destroy method to delete the record from the database
<?php
class EnteController extends Controller
{
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Ente $ente
* @return \Illuminate\Http\Response
*/
public function destroy(Ente $ente)
{
if(!$ente->actuacion()->exists()) {
$ente->delete();
return response()->json([
'msg' => '¡Ente eliminado exitosamente!',
'type' => 'success'
], 200);
}
return response()->json([
'msg' => 'Ha habido un error',
'description' => 'El ente no se ha podido eliminar debido a que está dentro de una o más actuaciones fiscales.',
'type' => 'error'
]);
}
}
In the network and console tab of the browser devtools I get this, and also an html laravel view of 404 not found
POST 127.0.0.1:8000/undefined 404 (Not Found)
This code is also present with other requests, if anyone know how could I solve this issue I would deeply appreciate it thanks in advance
if(boton.dataset.accion === 'eliminar') {
boton.addEventListener('click', (e) => {
Swal.fire({
title: '¿Desea eliminar el Ente seleccionado y sus dependencias?',
text: 'Esta opción no se puede deshacer.',
icon: 'warning',
showCancelButton: true,
confirmButtonText: 'Borrar',
cancelButtonText: 'Cancelar'
})
.then((result) => {
if(result.isConfirmed) {
const token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
const data = fetch(e.target.dataset.url, {
method: 'POST',
mode: 'cors',
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': token,
},
body: JSON.stringify({
_method: 'DELETE',
})
})
.then((response) => {
if (!response.ok) {
console.log(response);
throw new Error(`Ha fallado la petición, error: ${response}`);
}
return response.json();
})
.then((data) => {
Swal.fire({
title: data.msg,
text: data.description ?? '',
icon: data.type
}).then(() => location.reload());
})
.catch((error) => {
console.log(error);
});
EDIT: I'm inserting the url in a data attribute of a button in my view
<x-botones.boton title="eliminar ente o dependencias"
data-url="{{ route('entes.destroy', $ente->id_ente) }}"
data-accion="eliminar">
<i class="fas fa-trash" style="color: #ffffff;"></i>
</x-botones.boton>
Please or to participate in this conversation.