如何正确添加角度的标题?
这是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):
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如您所见:
因此,在您的情况下,您必须:
As you can see from the POST doc the second param is the data:
So in your case, you have to:
这样尝试:
try it like this:
我发现了这个问题。问题是您必须以不同的方式传递标题以获取发布请求并获取请求。通常,我发现了我想提出请求的解决方案。
对于发布请求,我在网络和此处找到了一个解决方案:
因此,在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:
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.