在 HTTP.get 中传递 headers

Passing headers in HTTP.get

我正在做一个项目,我正在处理 JWT,无论如何我已经尝试发送 post 请求并且它很有魅力但是当我尝试以相同的方式发出获取请求时是不工作。 这是我的代码:

ionViewWillEnter(){

this.storage.get('token').then((val) => {
 this.token = val 

console.log(this.token);
let link = Constants.API_ENDPOINT+"/v1/owner/section/list";
console.log("Debugging");
console.log(link);

this.http.get(link,{},{Authorization: "Bearer "+val})
.then(data =>{
  var hdrs = JSON.parse(data.headers);
  var res = JSON.parse(data.data);

  console.log(res);
  console.log(hdrs)
})
.catch(error =>{
  console.log("Yup Errors area ")
  console.log(error.status);
console.log(error.error); // error message as string
console.log(error.headers);

});
});
}

我得到以下输出:

 Debugging
(the correct link)
Yup Errors area ["a message I have set to make sure that the code    executed the catch method"]
undefined
undefined
undefined

所以基本上我得到一个错误,但 ionic 不会显示它!

注:

我在 Postman 中用相同的 headers 向同一个 link 发送了一个请求,它返回了正确的响应

我非常怀疑你的响应 HTTP headers 本身是 JSON 格式,这一行暗示了这一点:

var hdrs = JSON.parse(data.headers);

您的 headers 可能只是 key/value 对的映射,未 JSON 编码。我想知道该行是否抛出异常从而中止成功处理程序的其余部分。