vue-resource interceptor for auth headers

后端 未结 2 1030
独厮守ぢ
独厮守ぢ 2020-12-31 09:26

I am trying to set up a Vuejs fronted application (vue-cli webpack template) to sit on top of my Laravel API.

I am able to get a successful response from the API wit

2条回答
  •  萌比男神i
    2020-12-31 09:41

    It turns out my problem was the syntax for which I was setting the headers in the interceptor.

    It should be like this:

    Vue.use(VueResource)
    
    Vue.http.interceptors.push((request, next) => {
      request.headers.set('Authorization', 'Bearer eyJ0e.....etc')
      request.headers.set('Accept', 'application/json')
      next()
    })
    

    While I was doing this:

    Vue.use(VueResource)
    
    Vue.http.interceptors.push((request, next) => {
      request.headers['Authorization'] = 'Bearer eyJ0e.....etc'
      request.headers['Accept'] = 'application/json'
      next()
    })
    

提交回复
热议问题