如何正确添加角度的标题?

发布于 2025-01-22 07:07:21 字数 928 浏览 0 评论 0原文

这是Angular中的服务代码:

getcareer(dataout:any){
   let headers = new HttpHeaders().append('Content-Type', 'application/json').append('Authorization','Bearer'+' '+GlobalService.authtoken);
  //headers = headers.append('Content-Type', 'application/json').append('Authorization','Bearer'+' '+GlobalService.authtoken);
  //let headers={'Authorization':'Bearer'+' '+GlobalService.authtoken}
  console.log(headers);
  return this.http.post('http://localhost:3000/employee/getcareer',
  {'headers': headers })
  .subscribe(data => {
    dataout = data});
}

现在登录并尝试访问此页面后,它说401,未经授权。在浏览器控制台中,将标题添加到lazyupdate部分中,而不是标题部分。我尝试过.set .append和其他一些方法,但没有运气。请看看,谢谢。

编辑: 这是浏览器中的详细控制台(我在控制台日志中打印了httpheaders):

上面是图像的链接(对不起,在此处发布图像的问题)。您可以看到“标题”部分中的标题不存在,而是在“ Lazyupdate”部分中。

Here is the service code in Angular:

getcareer(dataout:any){
   let headers = new HttpHeaders().append('Content-Type', 'application/json').append('Authorization','Bearer'+' '+GlobalService.authtoken);
  //headers = headers.append('Content-Type', 'application/json').append('Authorization','Bearer'+' '+GlobalService.authtoken);
  //let headers={'Authorization':'Bearer'+' '+GlobalService.authtoken}
  console.log(headers);
  return this.http.post('http://localhost:3000/employee/getcareer',
  {'headers': headers })
  .subscribe(data => {
    dataout = data});
}

Now after logging in and trying to access this page, it says 401, Unauthorized. In the browser console, The headers are added in the LazyUpdate section instead of the headers section. I have tried .set .append and some other methods but no luck. Please have a look, thankyou.

Edit:
Here is a detailed console in browser (I have printed the httpheaders here in console log):

https://ibb.co/f002tZ9

Above is the link to the image (sorry there is an issue posting an image here). You can see the Headers are not present in 'header' section but in 'lazyupdate' section.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

生生漫 2025-01-29 07:07:21

如您所见:

/** POST: add a new hero to the database */
addHero(hero: Hero): Observable<Hero> {
  return this.http.post<Hero>(this.heroesUrl, hero, httpOptions)
    .pipe(
      catchError(this.handleError('addHero', hero))
    );
}

因此,在您的情况下,您必须:

  getcareer(dataout:any){
    const headers = new HttpHeaders().append('Content-Type', 'application/json').append('Authorization','Bearer'+' '+GlobalService.authtoken);
    console.log(headers);
    return this.http.post('http://localhost:3000/employee/getcareer',
    null,
    {headers }) //unncessary key-value
    .subscribe(data => dataout = data);
}

As you can see from the POST doc the second param is the data:

/** POST: add a new hero to the database */
addHero(hero: Hero): Observable<Hero> {
  return this.http.post<Hero>(this.heroesUrl, hero, httpOptions)
    .pipe(
      catchError(this.handleError('addHero', hero))
    );
}

So in your case, you have to:

  getcareer(dataout:any){
    const headers = new HttpHeaders().append('Content-Type', 'application/json').append('Authorization','Bearer'+' '+GlobalService.authtoken);
    console.log(headers);
    return this.http.post('http://localhost:3000/employee/getcareer',
    null,
    {headers }) //unncessary key-value
    .subscribe(data => dataout = data);
}

不念旧人 2025-01-29 07:07:21

这样尝试:

const options = {
  headers: {
    'content-type': 'application/json',
    Authorization: 'Bearer ' + GlobalService.authtoken
  }
};

this.http.post('http://localhost:3000/employee/getcareer', options)

try it like this:

const options = {
  headers: {
    'content-type': 'application/json',
    Authorization: 'Bearer ' + GlobalService.authtoken
  }
};

this.http.post('http://localhost:3000/employee/getcareer', options)
梦情居士 2025-01-29 07:07:21

我发现了这个问题。问题是您必须以不同的方式传递标题以获取发布请求并获取请求。通常,我发现了我想提出请求的解决方案。
对于发布请求,我在网络和此处找到了一个解决方案:

  const headers = { 'Authorization': 'Bearer '+GlobalService.authtoken, 'Content-Type': 'application/json' };


  return this.http.post('http://localhost:3000/employee/getcareer/',body,{headers}).subscribe(data => {
    this.dataout = data});

因此,在URL之后,主体出现在帖子请求中,然后是标头。如果没有身体,则将null留在那里,然后放置标题。与GET请求一样,没有任何尸体到来,并且在URL之后立即放置标头。

I found out the problem. the thing is that you have to pass headers differently for POST requests and GET requests. Normally i have found solutions that were for GET requests I assume.
For POST request I found a solution over the web as well as here:

  const headers = { 'Authorization': 'Bearer '+GlobalService.authtoken, 'Content-Type': 'application/json' };


  return this.http.post('http://localhost:3000/employee/getcareer/',body,{headers}).subscribe(data => {
    this.dataout = data});

so right after the URL, body comes in a POST request and then the headers. If there is no body then leave null there and then put headers. Where as in a GET request, no body comes and headers are put right after the URL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文