使用角度4中的body进行httpClient删除的问题

时间:2018-04-05 05:16:24

标签: angular httpclient

这里发出删除请求(角度为4),因为我得到了

  

requestoptionargs

类型中不存在请求标头

下面是我的代码:

myMethod(id: string, reason: string): Observable<any> {

        const reqHeaders = new Headers({
            'Content-Type': 'application/json'
          });
        const body =
            `{"reqId": "${id}",
             "reqReason": "${reason}"
            }`;
        return this.httpClient.delete(
            `http://localhost:188/.../1677777555`,
            new RequestOptions({ reqHeaders, body }))
            .map(rez => JSON.parse(rez))
            // .catch(error => this.handleError(error))
            ;
    }

在这一行是我得到错误new RequestOptions({ requestHeaders, body }))这里有任何明显的错误吗?

编辑: 另一个变种

与以前相同的身体:

const options = new RequestOptionsArgs({
    body: body,
    method: RequestMethod.Delete
  });

return this.httpClient.request('http://testAPI:3000/stuff', options);
}

现在我得到了:

  

RequestOptionsArgs引用一种类型,但在这里被用作值

2 个答案:

答案 0 :(得分:3)

根据文档尝试如下:https://angular.io/api/common/http/HttpClient#members

你可以试试像这样的一般要求

let options = new RequestOptionsArgs({ 
    body: body,
    method: RequestMethod.Delete
    headers : reqHeaders
  });

this.http.request('http://testAPI:3000/stuff', options)
    .subscribe((ok)=>{console.log(ok)});

答案 1 :(得分:2)

试试这个:

import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http';

myMethod(id: string, reason: string {
    let params = new HttpParams()
        .append('id', id)
        .append('reason', reason)
    let headers = new HttpHeaders({ 'Content-Type': 'application/JSON' });
    return this.httpClient.delete<any>('http://localhost:188/.../1677777555', { headers: headers, params: params })
    .map(rez => JSON.parse(rez));;