简体   繁体   中英

Angular http.post, response not getting, always getting response as null

Angular Http.post, Have one post call, I'm sending jsonData as a param with below formatted data, but everytime im getting response as null, could you please is there any mistake in my code. If any please help. Here is sample code I tried,

let jsondataItems :any =[];

var jsonData = [
    {
        "empId": 11234,
        "salary": "98763",
        "test1id": "9897989",
        "test2id": "657453",
        "test3id": "4456",
        "month": "AUG-19"
    }]

this.http
      .post<empData>(url, jsonData)
      .subscribe((res) => {
        this.jsondataItems = res;
        console.log("res" + this.jsondataItems); // getting null
        console.log("res" + JSON.stringify(this.jsondataItems)); //getting null
        console.log("Processed successfully!!!"); // getting console -- working
      });
  }



interface empData {
  empId: number; //1234
  salary: string; //"3456"
  test1id: string; //""5678"
  test2id:string; //"5643"
  test3id:string; // "4533"
  month: string; //"JAN-20"
}

Try the following:

 import { HttpHeaders } from '@angular/common/http';
        
        const httpOptions = {
          headers: new HttpHeaders({
            'Content-Type':  'application/json'
          })
        };
   
    let jsondataItems :any =[];
    
    var jsonData = [
        {
            "empId": 11234,
            "salary": "98763",
            "test1id": "9897989",
            "test2id": "657453",
            "test3id": "4456",
            "month": "AUG-19"
        }]
    
    this.http
          .post<empData>(url, jsonData,httpOptions)
          .subscribe((res) => {
            this.jsondataItems = res;
            console.log("res" + this.jsondataItems);
            console.log("res" + JSON.stringify(this.jsondataItems));
            console.log("Processed successfully!!!"); 
          });
      }
    
    
    
    interface empData {
      empId: number;
      salary: string; 
      test1id: string; 
      test2id:string; 
      test3id:string; 
      month: string; 
    }

You can take the same http options you have but add the observe: response. the header is HttpHeaders from http client. ex: headers: new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded'),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM