【问题标题】:error with authorization header angular http授权标头角度http错误
【发布时间】:2021-12-15 19:33:55
【问题描述】:

我在节点 js expree 中有后端,当你向我的后端提交邮递员申请时,我试图获取我所有的 product_types 这就是答案:

postman http request and header

但是当我在前端 angular 上做同样的请求时,我得到了一个错误

header Authorization is missing

这是我的 Angular 服务 http 请求

 getTypes(token:any):Observable<any>{
    let headers = new HttpHeaders().set('Content-Type', 'application/json')
                                   .set('Authorization', token);
    console.log(headers)
    return this._http.post(this.url + 'productType/getall', {headers:headers});
    
  }

这是发送到我的邮递员后端的标头

{
  authorization: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7InN1YiI6MSwibmFtZSI6ImFkbWluIiwic3VybmFtZSI6ImFkbWluIiwicm9sZSI6InVzZXJfYWRtaW4iLCJpYXQiOjE2MzU2ODA3ODZ9LCJpYXQiOjE2MzU2ODA3ODYsImV4cCI6MTYzNTc2NzE4Nn0.9e-sP6ZnUeYe0EEpuLyQmxvIu7-U1_oXLZlt76XbBs0',
  'user-agent': 'PostmanRuntime/7.28.3',
  accept: '*/*',
  'cache-control': 'no-cache',
  'postman-token': 'b97c28a2-f88e-4953-93d5-199fd1443d77',
  host: 'localhost:3002',
  'accept-encoding': 'gzip, deflate, br',
  connection: 'keep-alive',
  'content-length': '0'
}
这个来自我的角度前端

{                                                                                                                       
  host: 'localhost:3002',                                                                                               
  connection: 'keep-alive',                                                                                             
  'content-length': '476',                                                                                              
  'sec-ch-ua': '"Google Chrome";v="95", "Chromium";v="95", ";Not A Brand";v="99"',                                      
  accept: 'application/json, text/plain, */*',                                                                          
  'content-type': 'application/json',                                                                                   
  'sec-ch-ua-mobile': '?1',                                                                                             
  'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95
.0.4638.54 Mobile Safari/537.36',                                                                                       
  'sec-ch-ua-platform': '"Android"',                                                                                    
  origin: 'http://localhost:4200',                                                                                      
  'sec-fetch-site': 'same-site',                                                                                        
  'sec-fetch-mode': 'cors',                                                                                             
  'sec-fetch-dest': 'empty',                                                                                            
  referer: 'http://localhost:4200/',                                                                                    
  'accept-encoding': 'gzip, deflate, br',                                                                               
  'accept-language': 'es-ES,es;q=0.9,en;q=0.8'                                                                          
}                                                                                                                       
                                                                                                                        

我不知道为什么不使用令牌发送授权 令牌不为空

angular consol with the token and the error

【问题讨论】:

    标签: angular typescript https xmlhttprequest http-headers


    【解决方案1】:

    您的 HttpClient post 参数顺序不正确..

    post(url: string, body: any, options: { headers?: HttpHeaders....
    

    第二个参数是post 的主体,第三个是带有标题的选项

    或者,也许您应该使用 get - 在这种情况下,您应该保持参数不变

    【讨论】:

      猜你喜欢
      • 2018-04-01
      • 2015-11-27
      • 2018-09-11
      • 2011-06-08
      • 1970-01-01
      • 2018-04-13
      • 2015-05-30
      • 2016-04-24
      相关资源
      最近更新 更多