我正在尝试在 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) .
在此先感谢您的帮助。
最佳答案
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/