Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a nodeJS service and Angular2 Application. when I call a get method by passing headers to NodeJS , the headers are not coming. Can anyone please tell me where I'm doing it wrong.

Note: When call the same service by passing header from Fiddler it is working fine. Only problem is from Angular application.

What I have tried:

package.json
{
"name": "SampleApp",
"version": "1.0.0",
"scripts": {
"start": "tsc && concurrently \"tsc -w\" \"lite-server\" ",
"lite": "lite-server",
"tsc": "tsc",
"tsc:w": "tsc -w"
},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/angular/angular.io/blob/master/LICENSE"
}
],
"dependencies": {
"@angular/common": "~2.2.0",
"@angular/compiler": "~2.2.0",
"@angular/core": "~2.2.0",
"@angular/forms": "~2.2.0",
"@angular/http": "~2.2.0",
"@angular/platform-browser": "~2.2.0",
"@angular/platform-browser-dynamic": "~2.2.0",
"@angular/router": "~3.2.0",
"@angular/upgrade": "~2.2.0",
"angular-in-memory-web-api": "~0.1.15",
"bootstrap": "^3.3.7",
"core-js": "^2.4.1",
"reflect-metadata": "^0.1.8",
"rxjs": "5.0.0-beta.12",
"systemjs": "0.19.39",
"zone.js": "^0.6.25"
},
"devDependencies": {
"@types/core-js": "^0.9.34",
"@types/node": "^6.0.45",
"concurrently": "^3.0.0",
"lite-server": "^2.2.2",
"typescript": "^2.0.3"
}
}

---------------------------
app.component.ts
JavaScript
import { Component } from '@angular/core';
import { Http, Headers, RequestOptions, Response } from '@angular/http';

//Import RxJs required methods
// import {Observable} from 'rxjs/Rx';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/observable/throw';

import {Observer} from 'rxjs/observer';
// import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';

@Component({
  selector: 'my-app',
  templateUrl:'./app/app.component.html'
})

export class AppComponent { 
    firmID: string;
    flag: string;
    uKeys: string[];
    uris: string[] = ["http://localhost:8080/api/asps","http://wd-dbn-boops004/api/asps"];
    selectedUri: string;
    //hdr: Headers;
    hdrJson:Object;
    options: RequestOptions;
    
    constructor(private http: Http){
        this.selectedUri = this.uris[0];
        this.firmID="1";
        this.flag="-1";
        this.uKeys=["1405"];

        // this.hdr = new Headers({'content-type': 'application/json'});
        // this.hdr.append("auth-ticket","adafasf");
        // this.hdr.append("Accept", "application/json");
        // this.options = new RequestOptions({headers: this.hdr});
        //  this.hdrJson = JSON.stringify(this.hdr);
    }

    OnQuery(){
        let hdr = new Headers({'content-type': 'application/json','auth-ticket':'abcdd'});
        console.log("selected Uri: "+this.selectedUri+"; firmID: " + this.firmID + "; Flag: " + this.flag + "; Ukeys: " + this.uKeys);
        console.log("Calling service uri :"+this.selectedUri+"/"+this.firmID+"/"+this.uKeys);
        console.log("headers :"+ hdr);
        this.hdrJson = JSON.stringify(hdr);
        
        console.log(this.http.get(this.selectedUri+"/"+this.firmID+"/"+this.uKeys,{headers:hdr})
                        .map((res: Response) => res.json())
                        //.subscribe(res=>res.json()));
                        // .toPromise()
                        //.then(response => response.json().data)
                         .catch(this.handleError));                        
    }

    private handleError(error: any) {
          console.error('An error occurred', error); // for demo purposes only
          return error;
    }
}
Posted
Updated 23-Nov-16 0:28am
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900