Vue资源 - 401(未经授权)

时间:2017-03-01 07:35:34

标签: javascript vuejs2 vue.js vue-resource

browser console screenshot

当我调用方法“getUserData”时,我很遗憾地得到401(未经授权)“错误。但是如果使用GET调用URL”http://ppstemp.com/api/User/Profile“并且在Postman中使用相同的标题,则它可以正常工作! 我如何设置我的请求标题??

this.$http.get('http://ppstemp.com/api/User/Profile',{params:{
        n: ...
      }} , {
          headers: {
            "Authorization": "bearer "+ localStorage.getItem('token') ,
            "Accept": "application/json",
            "cache-control": "no-cache"
          }
        }).then(
             (response) => {
               // Handle data returned
                console.log(response.data);
            },
            //error callback
            (err) => console.log(err));
        }

1 个答案:

答案 0 :(得分:0)

Vue资源获取方法签名如下所示 -

this.$http.get('/someUrl', [options]).then(successCallback, errorCallback);

您需要将params与标题对象一起传递。

this.$http.get('http://ppstemp.com/api/User/Profile', {
  params: {
    n: ...
  },
  headers: {
    "Authorization": "bearer " + localStorage.getItem('token'),
    "Accept": "application/json",
    "cache-control": "no-cache"
  }
}).then(
  (response) => {
    // Handle data returned
    console.log(response.data);
  },
  //error callback
  (err) => console.log(err));
}