Vue.js 使用 vue-resource 记录 API 端点响应
Vue.js log API endpoint response with vue-resource
我正在尝试在我的组件中记录 API 响应只是为了测试目的,但我仍然收到错误 401(未经授权),我正在我的本地主机上做,我的 API 也托管在 localhost 中,但只是使用虚拟主机 url,所以我得到这个:http://api.myurl.dev
我是 运行 我的 vue localhost:8080 使用 vue-cli。
这是我的代码,我的 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”:)
也许您的 header (Authorization: Bearer eyJ0...
) 没有按照您的 API 预期的那样格式化,但是,再次关注您的 API 来调试它!
我正在尝试在我的组件中记录 API 响应只是为了测试目的,但我仍然收到错误 401(未经授权),我正在我的本地主机上做,我的 API 也托管在 localhost 中,但只是使用虚拟主机 url,所以我得到这个:http://api.myurl.dev
我是 运行 我的 vue localhost:8080 使用 vue-cli。
这是我的代码,我的 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”:)
也许您的 header (Authorization: Bearer eyJ0...
) 没有按照您的 API 预期的那样格式化,但是,再次关注您的 API 来调试它!