angularheadercorscustom-headers

Custom Header Issue (CORS)


I was made Web Application Using Angular 10 (Front-end) and Node.js(Backend). But when I send requests by adding Headers it was not working. (It was worked when Turn off CORS and send a request on Chrome) I was found lack some similar answers on StackOverflow but unfortunately, those are not worked for me :|

I sent a request as follows,

getAllSubmissions() {
    let token = sessionStorage.getItem('auth-token');
    let path = this.globe.base_path + 'get-submissions';
    this.http.get<any>(path, {headers:{'Access-Control-Allow-Origin':'*', 'auth-token':token}}).toPromise().then(data => {
      this.allsubmissoinsFiltered = data.filter(sub => sub.status != 'active')
      this.allSubmissionsCount = this.allsubmissoinsFiltered.length;
    }).catch(err => {
      console.log(err)
    })
  }

as well as, also i was try following code,

getAllSubmissions() {
    let token = sessionStorage.getItem('auth-token');
    var header = { headers: new HttpHeaders().set('auth-token', token) }
    let path = this.globe.base_path + 'get-submissions';
    this.http.get<any>(path, header).toPromise().then(data => {
      this.allsubmissoinsFiltered = data.filter(sub => sub.status != 'active')
      this.allSubmissionsCount = this.allsubmissoinsFiltered.length;
    }).catch(err => {
      console.log(err)
    })
  }

both types are not working properly.

How can I fix this issue? Hopefully waiting for your valuable response!


Solution

  • try following code,

    getAllSubmissions() {
        let token = sessionStorage.getItem('auth-token');
        var header = { headers: new HttpHeaders().set('Authorization', token) }
        let path = this.globe.base_path + 'get-submissions';
        this.http.get<any>(path, header).toPromise().then(data => {
          this.allsubmissoinsFiltered = data.filter(sub => sub.status != 'active')
          this.allSubmissionsCount = this.allsubmissoinsFiltered.length;
        }).catch(err => {
          console.log(err)
        })
      }
    

    this will be working.