how to set headers to application/json in angular2

前端 未结 5 1155
难免孤独
难免孤独 2021-01-17 03:00

I am trying to send HTTP post request in Angular2 but not able to set headers to content type application JSON.

My code is:

login(url,postdata)
{
            


        
5条回答
  •  梦毁少年i
    2021-01-17 03:50

    Referencing the Angular 2 Angular Http Guide @angular/http has been deprecated, and @angular/common/http should be the one you are using in your Angular 2 App. Because if you do not specify http headers the default request will be sent as Content-Type text/plain, How you modify the http headers is to:

    import { HttpClient, HttpHeaders } from '@angular/common/http';
    .....
    const req = this.http.post('/api/PostRequest/',
                                JSON.stringify(object),
                                {
                                 headers:new HttpHeaders()
                                 .set('Content-Type','application/json')
                                 }).subscribe();
    

提交回复
热议问题