【问题标题】:How to pass parameter in DELETE request in Angular2如何在Angular2的DELETE请求中传递参数
【发布时间】:2017-05-04 08:56:12
【问题描述】:

我正在尝试在 Angular 2 中创建 Delete 请求。

我所做的是这样的-

import { Component } from '@angular/core';
import { Http, RequestOptions, URLSearchParams } from '@angular/http';
//import { Http, Response, Headers, RequestOptions, URLSearchParams } from '@angular/http';
import { Profile } from '../../dataModel/Profile.ts';   //Data Model

@Component({
    selector: 'profile-list',
    template: require('./profileList.component.html'),
    styles: [require('./profileList.component.css')]
})

export class ProfileListComponent
{
    private _deleteProfileUrl: string = "/api/Profile/Delete";

    constructor(private http: Http)
    {
    }

    public deleteProfile(profileId)
    {
        this.http.delete(this._deleteProfileUrl, new RequestOptions({
            // Have to make a URLSearchParams with a query string
            search: new URLSearchParams('profileId=' + profileId) // <-----
        }));

        alert("Deleted - " + profileId);
    }
}

所以,我正在创建一个这样的删除请求-

this.http.delete(this._deleteProfileUrl, new RequestOptions({
        // Have to make a URLSearchParams with a query string
        search: new URLSearchParams('profileId=' + profileId) // <-----
    }));

但它不工作并且没有给出任何错误。

因为如果调用了deleteProfile函数,只有alert来了,而没有做任何AJAX请求。

因为在调用函数后我有这样的事情-

我保证 API 运行良好。

那么,谁能帮我解决我做错了什么?

此外-

我的完整代码可以在here (if needed)找到。

提前感谢您的帮助。

【问题讨论】:

    标签: javascript angular angular2-forms angular2-services


    【解决方案1】:

    Observables 是惰性的。即使您不想处理响应,您也必须订阅它们才能执行请求。所以可以这样写:

    this.http.delete(this._deleteProfileUrl, new RequestOptions({
         search: new URLSearchParams('profileId=' + profileId)
       })).subscribe(res => {
         alert("Deleted - " + profileId);
       });
    

    【讨论】:

      猜你喜欢
      • 2016-11-05
      • 2016-12-31
      • 1970-01-01
      • 2022-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-11
      • 2013-02-11
      相关资源
      最近更新 更多