[英]Vue.js log API endpoint response with vue-resource
i'm trying log the API response in my component just to test purpose, but i still get a error 401 (unauthorized), i'm doing it in my localhost, my API is hosted in localhost too, but just using virtual-host url, so i get this: http://api.myurl.dev 我正在尝试在我的组件中记录API响应只是为了测试目的,但我仍然得到错误401(未经授权),我在我的localhost中执行它,我的API也托管在localhost中,但只是使用虚拟主机url,所以我明白了: http : //api.myurl.dev
and i'm running my vue in localhost:8080 using vue-cli. 我正在使用vue-cli在localhost:8080中运行我的vue。
this is my code im my main.js : 这是我的代码我的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()
})
and my App.vue 和我的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>
Triggering your API through vue-resource, it is actually answering to your frontend application that you are not authorized to consume it, with an appropriate HTTP 401 status code. 通过vue-resource触发您的API,它实际上是通过适当的HTTP 401状态代码回答您的前端应用程序,您无权使用它。
Focus on your backend API! 专注于您的后端API! Your frontend application is running pretty well, and actually communicating with your API, which answers "401" :)
您的前端应用程序运行良好,并实际与您的API通信,它回答“401”:)
Maybe your header ( Authorization: Bearer eyJ0...
) is not formatted as expected by your API but, again, focus on your API to debug this! 也许您的标题(
Authorization: Bearer eyJ0...
)没有按照API的预期格式化,但是,再次关注您的API来调试它!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.