aimprogman
aimprogman

Reputation: 294

angular2 http delete

I use http.delete for remove my data.

This code in my service [MyService]:

deleteData(id){
  let headers = new Headers(),authtoken = localStorage.getItem('token');
     headers.append("Authorization", 'Bearer' + authtoken);
     headers.append('X-Requested-With', 'XMLHttpRequest')
     headers.append('Content-Type', 'application/json;')

    return this.http.delete('http://link/'+id, { headers: headers })
                  .map((resp:Response)=>resp.json())
                  .catch((error:any) =>{return Observable.throw(error);}); 
 } 

This code in my component:

private delete(id):void{
         console.log(id); //show`s id
         this.myService.deleteData(id)
         .subscribe((data) => {console.log(data)});
}

Show`s error "caused by: A network error occurred". My mistake was elsewhere. It is work.

Upvotes: 1

Views: 1259

Answers (1)

Jorawar Singh
Jorawar Singh

Reputation: 7621

There is nothing wrong in your code what i can see and you are not getting any error from server either. Try to call the same end point from postman and match the headers and check your Internet connection.

Upvotes: 1

Related Questions