自定义 Header 问题 (CORS)
Custom Header Issue (CORS)
我使用 Angular 10(Front-end)和 Node.js(后端)制作了 Web 应用程序。但是当我通过添加 Headers 发送请求时,它不起作用。 (关闭 CORS 并在 Chrome 上发送请求时有效)
我发现 Whosebug 上缺少一些类似的答案,但不幸的是,这些答案对我不起作用:|
我发送了如下请求,
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)
})
}
此外,我也尝试了以下代码,
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)
})
}
两种类型都无法正常工作。
我该如何解决这个问题?希望等待您宝贵的回应!
试试下面的代码,
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)
})
}
这会起作用。
我使用 Angular 10(Front-end)和 Node.js(后端)制作了 Web 应用程序。但是当我通过添加 Headers 发送请求时,它不起作用。 (关闭 CORS 并在 Chrome 上发送请求时有效) 我发现 Whosebug 上缺少一些类似的答案,但不幸的是,这些答案对我不起作用:|
我发送了如下请求,
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)
})
}
此外,我也尝试了以下代码,
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)
})
}
两种类型都无法正常工作。
我该如何解决这个问题?希望等待您宝贵的回应!
试试下面的代码,
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)
})
}
这会起作用。