miquios's avatar

Url request as "undefined " when performing fetch request

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

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>
0 likes
2 replies
Tray2's avatar

undefined usually means you have made a type-o in you code.

Probably here

e.target.dataset.url
1 like
miquios's avatar

@Tray2 I forgot to mention, it sometimes will work correctly, sometimes I'll click and send the DELETE request and the "ente" is gonna be deleted (I also know it worked correctly because the Sweetalert is triggered), but sometimes I'll get the 404 response

Please or to participate in this conversation.