Why does DELETE give error?

Asked

Viewed 96 times

0

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

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');
    });
  }

CONTROLLER CUSTOMERS:

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');
  • cadastro_clientes.php returns what a json? like it is? tried to give a console.log(data) in .error()?

  • It just doesn’t return anything to me. It already falls right into the error.

  • He even deletes, because I see in DB he deletes. And in error it gives: {readyState: 4, getResponseHeader: ?, getAllResponseHeaders: ?, setRequestHeader: ?, overrideMimeType: ?, ... } abort : teis(a) Always : and then#Xa;complete : and then##Xa() done : ĩ() error : ľ() fail : , and#Xa;and#Xa;and#Xa;sponHeaders #############a;, #getResponseHeader&###Xa;####Xa;######Xa;, #######Xa;, (#####Xa;, ####Xa;, #########Xa;, #########Xa;, #Xa;, ###### pipe

  • In the cadastro_clientes.php leave as last line echo json_encode(array('teste'=> 'teste...')); and puts console.log(data) in the error()

  • Is this not some sort of Laravel configuration? Because I’m using this php Restfull api

  • Which jQuery version is using?

  • Instead of model::find(id) try $client =model::findOrFail(id) and then give a dd($client); and treat the return

Show 2 more comments

1 answer

1


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

public function destroy($id){

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

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

   $clientes->delete();
}

I saw in the tutorial the author forcing a 204:

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

Another try 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 much as it does not solve put in the comments the errors that will return in the console or Alert.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.