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 => { },
() => { }
);
The HTTP DELETE method is used to delete a resource from the server. Unlike GET and HEAD requests, the DELETE requests may change the server state. Sending a message body on a DELETE request might cause some servers to reject the request. But you still can send data to the server using URL parameters.
You can send payload along with the DELETE request as part of the params in the options object as follows: this. http. delete('http://testAPI:3000/stuff', { params: { data: yourData }).
The short answer is: You should include a response body with an entity describing the deleted item/resource if you return 200.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With