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

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

问题描述

我正在尝试与来自 Angular 2 前端的某种 RESTful API 进行对话.

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

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

然而,Angular 2 的 Http 模块并不完全同意带有正文的 DELETE 请求,并尝试发出此请求

let headers= new Headers();headers.append('access-token', token);让 body= JSON.stringify({目标:targetId,子集:水果",原因:烂"});让 options= new RequestOptions({headers:headers});this.http.delete('http://testAPI:3000/stuff', body,options).subscribe((ok)=>{console.log(ok)});<------第67行

给出这个错误

app/services/test.service.ts(67,4): 错误 TS2346: 提供的参数与调用目标的任何签名都不匹配.

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

是否有更好的方法来发送这些数据?

或者我应该将其转储到标题中并收工?

对这个难题的任何见解将不胜感激

解决方案

@angular/http 在 http.js 中的定义:

<块引用>

删除(网址,选项)

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

我发现了另一个主题,其中包含对相应 RFC 的引用,其中包括:如何在ajax DELETE中传递数据请求头以外的请求

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

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.

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

gives this error

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

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

解决方案

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

delete(url, options)

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

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天全站免登陆