角度删除请求无效

问题描述 投票:0回答:3

如果你能帮我解决这个问题,我不知道为什么不工作..

我正在使用asp.net mvc作为我的后端使用angular 4,使用邮递员,我的删除请求有效,但在角度,我无法弄清楚为什么不是,并且没有错误...

我正在建立一个小角度crud应用程序,并尝试从我填充的表中的同一行获取用户ID ...

我的控制台或编译器中没有任何错误,但是当我点击一个触发我的组件中的删除功能的按钮时似乎没有发生任何事情...这里是代码

 export class user{
 Id: string;
 Name: string;
 }

在我的服务中,我有一个正常运行的get和post方法,但删除不起作用,代码:

    deleteUser(id: string) {
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });
    return this.http.delete('http://localhost:48622/user/' + id, options)
        .map(res => res.json());

    }

在我的component.ts中

   userlist: user[];

   DeleteUser(id: string) {
    if (confirm('Are you sure to delete this record ?') == true) {
        this._usermanagementservice.deleteUser(id)
            .subscribe(x => {
          //this populates table with names, function is working, not sure should I call it this way so my table can be updated?
                this.LoadAllUsers();
            })
      }
    }

在我的app.component.html中,我认为是主要问题..

注意到我只想在同一行显示带删除按钮的用户名...

      <table style="width:100%" class="table table-striped" 
      id="billing_history_table">
      <thead class="head">
        <tr>
            <th>User Name</th>

            <th></th>

        </tr>
        </thead>
        <tbody>
        <tr *ngFor="let u of userlist">
            <td>{{u.Name}}</td>
             <td><a (click)="DeleteUser(e.id)"><span class="glyphicon 
         glyphicon-trash"></span></a></td>
        </tr>
          </tbody>
      </table>
angular http-delete
3个回答
0
投票

你的代码应该不读

DeleteUser(u.Id)

注意:u.Id而不是e.id.

您能否确认正确的ID正在传递给您的API


0
投票

有一个错字变化DeleteUser(e.id)DeleteUser(u.Id)

这就是它的样子

 <table style="width:100%" class="table table-striped" 
  id="billing_history_table">
  <thead class="head">
    <tr>
        <th>User Name</th>

        <th></th>

    </tr>
    </thead>
    <tbody>
    <tr *ngFor="let user of userlist">
        <td>{{user.Name}}</td>
         <td><a (click)="DeleteUser(user.Id)"><span class="glyphicon 
     glyphicon-trash"></span></a></td>
    </tr>
      </tbody>
  </table>

尝试给变量赋予适当的名称,使调试变得容易


0
投票

问题是,在我的数据库中,列名为userId而不是id,所以当我在我的html DeleteUser(u.id)中更改为DeleteUser(u.userId)时,它工作了!

© www.soinside.com 2019 - 2024. All rights reserved.