5

Could Angular2 send json body via http method delete ?

I try it and it said error

ORIGINAL EXCEPTION: options.search.clone is not a function

service.ts

deletetag(tagid: number): Observable<any> {

    let body = JSON.stringify(
        {
            "token": "test",
            "content": {
                "tagId": tagid
            }
        }
    );

    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });

    return this.http.delete("http://localhost:8080/backend/tag", body, options)
        .map(res => this.extractData(res))
        .catch(this.handleError);
}

component.ts

this.tagService.deletetag(1)
   .subscribe(
      data => { },
      error => { },
      () => { }
   );

1 Answer 1

10

As per RequestOptionsArgs interface and Http delete function argument, i think you need to send delete request as below :

deletetag(tagid: number): Observable<any> {

  let body = JSON.stringify(
      {
        "token": "test",
        "content": {
        "tagId": tagid
      }
    }
  );
  let headers = new Headers({ 'Content-Type': 'application/json' });
  let options = new RequestOptions({
    headers: headers,
    body : body
  });

  return this.http.delete("http://localhost:8080/backend/tag", options)
        .map(res => this.extractData(res))
        .catch(this.handleError);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Although this may work in angular, you may have issues server side with this: stackoverflow.com/a/25237188/3806701
in Angular 5, the RequestOptions is DEPRECATED, see angular.io/api/http/RequestOptions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.