Warm tip: This article is reproduced from stackoverflow.com, please click
ionic-framework typescript

Headers are not sending Ionic

发布于 2020-04-10 16:09:24

My Ionic app is not sending the Headers. Here is my Code.

async setAnzahlTische() {
let headers1 = new Headers();
headers1.append('Accept', 'application/json');
headers1.append('Content-Type', 'application/json' ); 
headers1.set('Authorization', await this.storage.get('token'));
headers1.append('Access-Control-Allow-Origin', '*');
headers1.append('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
headers1.append('x-Trigger', 'CORS');

let tischid = 2;


let postData =  {

    'tischid' : tischid

  };
let tos = JSON.stringify(postData);

this.http.post('http://127.0.0.1:8000/updatedata', tos, {responseType: 'text'})
    .subscribe(data => {
        console.log(data);
        //this.router.navigate(['/home']);

    }, error => {
        console.log(error.message);
    });
}

So what am I missing? When I do headers1.get('Authorization') I get the Token I want. Everything works fine in Postman.

Questioner
rotschopf
Viewed
65
Jain Bhavesh 2020-02-03 00:10

Just do it like that it works finr

 this.http.post("YOUR URL", "PARAMETER", { headers: { Auth: token } });