Angular2中的Http.DELETE请求主体 [英] Body of Http.DELETE request in Angular2

查看:181
本文介绍了Angular2中的Http.DELETE请求主体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图从Angular 2前端与某种RESTful API进行对话.

I'm trying to talk to a somewhat RESTful API from an Angular 2 frontend.

要从集合中删除某些项目,除了可删除的唯一ID(可以附加到url)之外,我还需要发送其他一些数据,即身份验证令牌,一些集合信息和一些辅助数据.

To remove some item from a collection, I need to send some other data in addition to the removée unique id(that can be appended to the url), namely an authentication token, some collection info and some ancilliary data.

我发现最简单的方法是将身份验证令牌放入请求标头中,并将其他数据放入正文中.

The most straightforward way I've found to do so is putting the authentication token in the request Headers, and other data in the body.

但是,Angular 2的Http模块不太赞成带有主体的DELETE请求,并试图发出此请求

However, the Http module of Angular 2 doesn't quite approve of a DELETE request with a body, and trying to make this request

let headers= new Headers();
headers.append('access-token', token);

let body= JSON.stringify({
    target: targetId,
    subset: "fruits",
    reason: "rotten"
});

let options= new RequestOptions({headers:headers});
this.http.delete('http://testAPI:3000/stuff', body,options).subscribe((ok)=>{console.log(ok)}); <------line 67

给出此错误

app/services/test.service.ts(67,4): error TS2346: Supplied parameters do not match any signature of call target.

现在,我在语法上做错了吗?我很确定RFC会支持DELETE正文

Now, am I doing something wrong syntax-wise? I'm pretty sure a DELETE body is supported per RFC

有没有更好的方法来发送数据?

Are there better ways to send that data?

还是我应该将其转储到标头中并命名为一天?

Or should I just dump it in headers and call it a day?

任何对这个难题的见解都将受到赞赏

Any insight on this conundrum would be appreciated

推荐答案

来自 @ angular/http 的http.js中的定义:

Definition in http.js from the @angular/http:

删除(网址,选项)

delete(url, options)

该请求不接受正文,因此您似乎唯一的选择是,但您的数据必须在URI中.

The request doesn't accept a body so it seem your only option is to but your data in the URI.

除其他事项外,我发现了另一个主题,带有与RFC对应的引用: 如何在ajax中传递数据DELETE除标头以外的其他请求

I found another topic with references to correspond RFC, among other things: How to pass data in the ajax DELETE request other than headers

这篇关于Angular2中的Http.DELETE请求主体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆