如何在 Angular2 的 DELETE 请求中传递参数
How to pass parameter in DELETE request in Angular2
我正在尝试在 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)。
在此先感谢您的帮助。
Observables 是惰性的。即使您不想处理响应,您也必须订阅它们才能执行请求。所以可以这样写:
this.http.delete(this._deleteProfileUrl, new RequestOptions({
search: new URLSearchParams('profileId=' + profileId)
})).subscribe(res => {
alert("Deleted - " + profileId);
});
我正在尝试在 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)。
在此先感谢您的帮助。
Observables 是惰性的。即使您不想处理响应,您也必须订阅它们才能执行请求。所以可以这样写:
this.http.delete(this._deleteProfileUrl, new RequestOptions({
search: new URLSearchParams('profileId=' + profileId)
})).subscribe(res => {
alert("Deleted - " + profileId);
});