javascript - 如何在 Angular2 的 DELETE 请求中传递参数

标签 javascript angular angular2-forms angular2-services

我正在尝试在 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 请求完成。

因为在函数被调用后我有这样的东西-

enter image description here

我保证 API 运行良好。

所以,任何人都可以帮我解决我做错了什么吗?

此外-

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

在此先感谢您的帮助。

最佳答案

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

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

关于javascript - 如何在 Angular2 的 DELETE 请求中传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41220346/

相关文章:

javascript - 在运行时更改折线图的域

javascript - chrome 扩展中未定义 jquery

javascript - npm WARN 已弃用 npmconf@2.1.2 : this package has been reintegrated into npm and is now out of date with respect to npm

Angular 5 文件保存程序将 CSV 文件编码为 ANSI

用于移动 Web 应用程序的 JavaScript 模板引擎

javascript - Angular 4 - IE11 : font-awesome is not working with angular2-multiselect-dropdown on IE11

eclipse - 您必须在 Angular CLI 项目中才能使用 serve 命令。运行动态 Web 项目时出错

angular2-template - 如何根据某些条件在 Angular 2 中使文本框只读/可编辑

forms - Angular 2 中的表单未发送选择和复选框输入

angular - 从 Angular 2 中的 jquery 调用 TypeScript 方法