角 - $ http.delete返回成功,但不工作 [英] Angular - $http.delete returns success but doesn't works

查看:144
本文介绍了角 - $ http.delete返回成功,但不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个角+ PHP应用程序,我有问题要删除的客户。

I have an Angular + PHP application and I'm having problem to delete an customer.

我被没有错误,实际上,应用程序返回sucecess,但它并没有从我的数据库删除客户

I got no error, actually, the application returns sucecess, but it doesn't delete the customer from my database.

这是我的角度控制器

    app.controller('consultar_cliente_controller', function($scope, $http){
    $scope.listaDeCliente = [];

    var init = function(){
        $scope.buscar();
    };

    $scope.buscar = function(){
        $http.get('consultar_cliente.php')
             .success(function(data){
                $scope.listaDeCliente = data;
             })
             .error(function(){
                console.error('Erro ao executar o GET do cliente');
             });
    };

    $scope.deletar = function(id){
        $http.delete('remover_cliente.php/' + id)
             .success(function(){
                console.log('Cliente removido com sucesso!');
                $scope.buscar();
             })
             .error(function(){
                console.error('Erro ao remover cliente');
             })
    };

    init();
});

这是我的PHP

<?php
  $dbh = new PDO('pgsql:host=localhost;dbname=livraria_glp', 'postgres');
  /*
  * Recuperando todos os detalhes da requisição HTTP do Angular
  */ 
  $postdata = file_get_contents("php://input");
  $request  = json_decode($postdata);
  @$id      = $request->id;

  echo $id;

  $dbh->exec("DELETE FROM PRODUTO WHERE ID = '$id'") or die( $dbh->errorInfo() );

?>

这是我的HTML

And this is my HTML

<div class="well">
  <div class="container">
      <h2>Dados</h2>
      <div class="form-group">
        <label>Nome</label>
          <input class="form-control" type="text" ng-model="filtroNome" />
      </div>
  </div>
  <div class="container resultado">      
    <table class="table">
      <thead>
        <tr>
          <th>Nome</th>
          <th>CPF</th>
          <th>E-mail</th>
        </tr>
      </thead>
      <tbody>
        <tr ng-repeat="cliente in listaDeCliente | filter : filtroNome">
          <td>{{cliente.nome}}</td>
          <td>{{cliente.cpf}}</td>
          <td>{{cliente.id}}</td>
          <td><button class="btn btn-danger" ng-click="deletar(cliente.id)">Deletar</button></td>
        </tr>
      </tbody>
    </table>
  </div>
  <button class="btn btn-success" onclick="window.location.href='javascript:window.history.go(-1)'">Voltar</button>
</div>

编辑:我已经PHP尝试这样的:

I've tried this on PHP:

echo "DELETE FROM PRODUTO WHERE ID = '$id'";

和它返回:

DELETE FROM PRODUTO WHERE ID =''

DELETE FROM PRODUTO WHERE ID = ''

为什么不能PHP接受她的ID?

Why can't the PHP recieve the id?

推荐答案

角不发送请求主体的删除所以你将不得不读 ID 从URL。你可能也只是把它作为一个查询参数

Angular doesn't send a request body for DELETE so you're going to have to read id from the URL. You might as well just pass it as a query parameter

$http.delete('remover_cliente.php', {
    params: {id: id}
})

然后通过 $阅读_ GET ['身份证']

<?php
if (!isset($_GET['id'])) {
    exit;
}

$id = $_GET['id'];
$dbh = new PDO('pgsql:host=localhost;dbname=livraria_glp', 'postgres');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$stmt = $dbh->prepare('DELETE FROM PRODUTO WHERE ID = ?');
$stmt->execute([$id]);

echo $id;


另外(以防万一 $ _ GET 不起作用删除请求),你可以尝试强制使用该请求消息数据


Alternatively (and just in case $_GET doesn't work for DELETE requests), you could try forcing the request message data using

$http.delete('remover_cliente.php', {
    data: {id: id}
})

不过,我不知道这是不是支持,它肯定不是很REST-FUL。如果它的工作,你可以继续使用的file_get_contents(PHP://输入') json_de code()

这篇关于角 - $ http.delete返回成功,但不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆