gpt4 book ai didi

javascript - HttpParams 设置为 null 进行调用

转载 作者:行者123 更新时间:2023-12-01 00:47:56 25 4
gpt4 key购买 nike

将项目从 Angular 7 迁移到 Angular 8 后,我遇到了 HttpParams 和 HttpHeaders 问题。当我调用 API 时,未添加参数。如果有人能帮助我解决这个问题,那就太好了。

这是我定义 header 和参数的方法。

fetchJson(url: string, parameters ? : any) {
this.token = this.cookieService.get('access_token');
this.contrat_token = this.cookieService.get('contrat_token');

let headers = new HttpHeaders();
headers = headers.append('Content-Type', 'application/json');
headers = headers.append('Authorization', 'Bearer ' + this.token);
headers = headers.append('contrat_token', this.contrat_token);

let params = new HttpParams()
params.set('search', parameters);
console.log('les headers');
console.log(headers);
console.log('params');
console.log(params.toString())

return this._http.get(url, {
headers,
params
}).pipe(map((resp: any) => {
if (resp.status === 401 || resp.status == 401 || resp.status.toString() == "401") {
this.clearCookie();
} else {
let reponse = resp;

if (reponse == -1 || reponse == "-1") {
this.router.navigate(["/"]);
}
}

return resp;
}

我在我的服务中调用此方法,如下所示。

   getDetailThematiquePrevNext(id: string, typeBase: string) {
let URL = this.urlDecorator.urlAPIDecorate("DI", "GetDetailThematiqueHeaderPrevNext");
let params = this.urlDecorator.generateParameters({
id: id,
typeBase: typeBase,

});
return this.apiFetcher.fetchJson(URL, params);
}

最佳答案

Cue 提供的原因是正确的,您需要使用链接或执行您对 header 所做的操作

let headers = new HttpHeaders();
headers = headers.append('Content-Type', 'application/json');
headers = headers.append('Authorization', 'Bearer ' + this.token);
headers = headers.append('contrat_token', this.contrat_token);

let params = new HttpParams()
params = params = params.set('search', parameters);

更易读的编写方式如下

const headers = new HttpHeaders()
.append('Content-Type', 'application/json')
.append('Authorization', 'Bearer ' + this.token)
.append('contrat_token', this.contrat_token);

const params = new HttpParams().set('search', parameters);

此外,您可以删除 Content-Type header ,因为默认情况下它是 json

关于javascript - HttpParams 设置为 null 进行调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57227934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com