[英]VueJS GET request with Bearer token
我正在使用 VueJS 試驗 Kentico Delivery Preview API,它允許您通過提交不記名令牌進行授權來獲取未發布的內容( https://developer.kenticocloud.com/reference#authentication )。 但是,無論我做什么,我都會收到 401 響應。 PROJECT_ID、ITEM_NAME 和 TOKEN 都是正確的,取自項目,所以這不是錯字問題。 我承認我對身份驗證沒有太多經驗,但任何幫助將不勝感激:
var app = new Vue({
el: '#app',
data: {
message: 'Hello Vue!'
},
mounted () {
axios
.request({
url: '/items/ITEM_NAME',
method: 'get',
baseURL: 'https://preview-deliver.kenticocloud.com/PROJECT_ID',
headers: {
'Authorisation': 'Bearer TOKEN'
}
})
.then(response => {
console.log(response.data)
})
}
})
正如 Walter 在評論中指出的那樣,我用 S 而不是 Z 拼寫了 Authorization .. 因為我是英國人。 哎呀。
在您的請求之前使用 create 配置 axios 標頭
const TOKEN = 'Token';
const BASEURL = 'https://preview-deliver.kenticocloud.com/PROJECT_ID';
const ENDPOINT = '/items/ITEM_NAME';
axios.create({
baseURL: BASEURL,
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer '+TOKEN
}
})
.get(ENDPOINT)
.then(res => {
console.log(res);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.