[英]Vue.js + vue-resource + vue-router = Lexical declaration error?
[英]Vue.js log API endpoint response with vue-resource
我正在嘗試在我的組件中記錄API響應只是為了測試目的,但我仍然得到錯誤401(未經授權),我在我的localhost中執行它,我的API也托管在localhost中,但只是使用虛擬主機url,所以我明白了: http : //api.myurl.dev
我正在使用vue-cli在localhost:8080中運行我的vue。
這是我的代碼我的main.js :
import Vue from 'vue'
import VueRouter from 'vue-router'
import VueResource from 'vue-resource'
import Vuex from 'vuex'
import router from './routes/router.js'
import App from './App.vue'
Vue.use(VueRouter)
Vue.use(VueResource)
Vue.use(Vuex)
/* eslint-disable no-new */
// Start
new Vue({
router,
el: '#app',
render: h => h(App)
})
// Bearer token auth
Vue.http.interceptors.push((request, next) => {
request.headers.set('Authorization', 'Bearer eyJ0...')
request.headers.set('Accept', 'application/json')
next()
})
和我的App.vue
<template>
<div id="app">
<img src="./assets/logo.png" alt="">
<button type="button" @click="getFilial">click</button>
</div>
</template>
<script>
export default {
data () {
return {
context: 'app context',
loaded: false
}
},
methods: {
getFilial () {
this.$http.get('http://api.myurl.dev/educational/filials').then(response => {
console.log(response.data)
})
}
}
}
</script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
通過vue-resource觸發您的API,它實際上是通過適當的HTTP 401狀態代碼回答您的前端應用程序,您無權使用它。
專注於您的后端API! 您的前端應用程序運行良好,並實際與您的API通信,它回答“401”:)
也許您的標題( Authorization: Bearer eyJ0...
)沒有按照API的預期格式化,但是,再次關注您的API來調試它!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.