Why does DELETE give error?

0

I have the following function in jquery that gives the delete in the registry. It even deletes the record but does not go to success , it goes to error . Why?

function deletar_cliente(id_cliente)
  {
    var mensagem_sucesso = "";
    var mensagem_erro = "";
    var url_cliente = "cadastro_clientes.php";
    $.ajax({
      url: url_base + "clientes/" + id_cliente,
      type: 'DELETE',
      dataType: 'json',
    }).success(function(data)
    {
      console.log(data);
    }).error(function(data)
    {
      console.log('erro');
    });
  }

CLIENTS CONTROLLER:

public function destroy($id)
{

    $clientes = Clientes::find($id);

    if(!$clientes) {
      return response()->json([
        'message'   => 'Cliente não encontrado',
      ], 404);
    }

    $clientes->delete();
}

ROUTES:

use Illuminate\Http\Request;

Route::get('/', function () {
  return response()->json(['message' => 'Rep Api', 'status' => 'Connected']);;
});

Route::resource('clientes', 'ClientesController');
Route::resource('fornecedores', 'FornecedoresController');
Route::resource('usuarios', 'UsuariosController');
Route::resource('estados', 'EstadosController');
Route::resource('municipios', 'MunicipiosController');
Route::post('autenticacao', 'AuthController@authenticate');
Route::get('get-municipios/{estados_id}', 'MunicipiosController@getMunicipios');
    
asked by anonymous 04.10.2017 / 22:49

1 answer

1

In your controller, force it to return a message of success:

public function destroy($id){

$clientes = Clientes::find($id);

   if(!$clientes) {
     return response()->json([
       'message'   => 'Cliente não encontrado',
     ], 404);
   }

   $clientes->delete();
}

I saw the tutorial the author forcing a 204:

return response()->json($clientes->delete(), 204);

Another attempt you can use:

$.ajax({
    type: 'POST',
    dataType: "json",
    cache: false,
    url: url_base + "clientes/" + id_cliente,
    success: function (response) {
        console.log(response);
    },
    error: function (response) {
        alert('Error - ' + response.responseText);
        console.log(response);
    }
});

As long as it does not resolve the comments, the errors that will return in the console or alert.

    
05.10.2017 / 00:40