HttpClient不发送授权头[Angular]

时间:2018-05-21 22:26:16

标签: angular cors authorization httpclient

我尝试使用Angular的HttpClient发送带有授权标头的请求,但标头未成功发送。

这是我的代码。

return this.http.get( API_URL + '/validate-email/' + email, this.getToken());

这是我的仇敌:

General:
Request URL: http://10.0.2.35:3000/validate-email/
Request Method: GET
Status Code: 401 Unauthorized
Remote Address: 10.0.2.35:3000
Referrer Policy: no-referrer-when-downgrade


Request Headers:
Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate
Accept-Language: es-ES,es;q=0.9,gl;q=0.8,en;q=0.7,pt;q=0.6,la;q=0.5
Connection: keep-alive
Host: 10.0.2.35:3000
Origin: http://localhost:4200
Referer: http://localhost:4200/candidate-register
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) 
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36



Response Headers: 
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, 
Accept
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 49
Content-Type: application/json; charset=utf-8
Date: Mon, 21 May 2018 22:19:28 GMT
ETag: W/"31-iT0RTO+b5PSz1cX7BhK1mfQ8R6o"
X-Powered-By: Express

有谁知道错误是什么?

2 个答案:

答案 0 :(得分:0)

尝试将其更改为POST请求。

return this.http.post( API_URL + '/validate-email/' + email, this.getToken());

您将令牌包含在正文中,但在GET请求中没有对正文进行任何操作。

更多信息:https://stackoverflow.com/a/983458/6544475

答案 1 :(得分:0)

您还没有提到在标题中设置test1的任何地方。执行以下操作 this.getToken()
然后使用像 let requestHeaders = new HttpHeaders(); requestHeaders = requestHeaders.set('authorization', this.getToken());

有关详细信息Angular HttpClient doesn't send header

,请参阅此处