duangan4070 2016-07-18 12:12
浏览 34
已采纳

在PHP中删除Angular2应用程序中的数据

How would one delete data from a MySql database using PHP code in an Angular2 application? The closest advice is for Angular 1 and is as follows:

$scope.deleteProduct = function(id){

    // ask the user if he is sure to delete the record
    if(confirm("Are you sure?")){
        // post the id of product to be deleted
        $http.post('delete_product.php', {
            'id' : id
        }).success(function (data, status, headers, config){

            // tell the user product was deleted
            Materialize.toast(data, 4000);

            // refresh the list
            $scope.getAll();
        });
    }
}

Is it possible to use the post method similarly:

import { Injectable } from '@angular/core';
import { Http, Response, Headers } from '@angular/http';
import 'rxjs/Rx';

@Injectable()
export class HttpService {

  constructor(private  http: Http) {}

  deleteData() {
    return this.http.post('delete_record.php')         
  }
}

Any insight/experience with Angular2/PHP would be appreciated.

展开全部

  • 写回答

1条回答 默认 最新

  • douchen4915 2016-07-19 01:47
    关注

    Yes, the http post works similarly in angular2. Since you want to use post, i guess you also want to add a body to the request.

    import { Injectable } from 'angular/core';
    import { Http } from 'angular/http';
    
    @Injectable()
    export class HttpService {
    
      constructor(private  http: Http) {}
    
      deleteData(data: SomeObject) {
        let url = "delete_record.php";
        let body = JSON.stringify(data);
    
        return this.http.post(url, body)
           .subscribe(
              result => console.log(result),
              error => console.error(error)
           );
      }
    }
    

    You can also send a delete request, which would be "best practice".

     return this.http.delete(url)
            .subscribe(
               result => console.log(result),
               error => console.error(error)
            });
    

    More about the http-client here https://angular.io/docs/ts/latest/guide/server-communication.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部