Vue.js Node.js Axios.js在文档中无法正常工作

时间:2017-10-17 17:18:38

标签: node.js vue.js axios

环境:axios 0.16.2。节点4.0.8

使用时,简单的get就会引发错误:

axios.defaults.baseURL = 'http://localhost:3000/api/'
url :axios.get('/shoppinglist/s')

但如果我只使用完整网址

,它运行良好
axios.get('http://localhost:3000/api/shoppinglists/'

api.index.js

const axios = require('axios')

const config = {
  timeout: 1000
}

axios.defaults.baseURL = 'http://localhost:3000/api/'

export default {

  fetchShoppingLists: () => {
    return axios.get('/shoppinglists/', config)
    .catch(error => {
      console.log('FETCH error: ', error)
      if (error.response) {
        console.log('FETCH error.response: ', error.response)
      } else {
        console.log('Error', error.message)
        console.log(error.config)
      }
      throw error
    })
  }
}

的console.log

LOG LOG: 

'FETCH error: ', Error{
config: Object{adapter: null, transformRequest: Object{0: ...}, 
transformResponse: Object{0: ...}, 
timeout: 1000, xsrfCookieName: 'XSRF-TOKEN', xsrfHeaderName: 
'X-XSRF-TOKEN', maxContentLength: -1, 
validateStatus: function validateStatus(status) { ... }, 
headers: Object{Accept: ...}, 
baseURL: 'http://localhost:3000/api/', method: 'get', 
url: 'shoppinglists/', 
data: undefined}, 
response: Object{status: 404, config: Object{adapter: ..., transformRequest: ..., transformResponse: ..., timeout: ..., xsrfCookieName: ..., xsrfHeaderName: ..., maxContentLength: ..., validateStatus: ..., headers: ..., baseURL: ..., method: ..., url: ..., data: ...}, data: undefined}}


'FETCH error.response: ', Object{
status: 404, 
config: Object{adapter: null, transformRequest: Object{0: ...}, transformResponse: Object{0: ...}, 
timeout: 1000, xsrfCookieName: 'XSRF-TOKEN', 
xsrfHeaderName: 'X-XSRF-TOKEN', maxContentLength: -1, 
validateStatus: function validateStatus(status) { ... }, 
headers: Object{Accept: ...}, 
baseURL: 'http://localhost:3000/api/', 
method: 'get', 
url: 'shoppinglists/', data: undefined
}, 

data: undefined}

2 个答案:

答案 0 :(得分:1)

  

使用时,简单的get就会引发错误:

axios.defaults.baseURL = 'http://localhost:3000/api/'
url :axios.get('/shoppinglist/')
     

但如果我只使用完整网址

,它运行良好      

axios.get('http://localhost:3000/api/shoppinglists/'

您是否尝试从baseURL中删除尾随的/?例如http://localhost:3000/api

我会假设它正在尝试http://localhost:3000/api/ + /shoppinglists/s = http://localhost:3000/api//shoppinglists

答案 1 :(得分:0)

问题解决了!我发现了这个问题:

1 / axios如何发送请求

 LOG: 'AXIOS DISPATCH REQUEST 51: ', '{"transformRequest":{},"transformResponse":{},"
 timeout":0,"xsrfCookieName":"XSRF-TOKEN","xsrfHeaderName":"X-XSRF-TOKEN","maxContentLength":-1,"headers":{"Accept":"application/json, text/plain, */*"},"baseURL":"http://localhost:3000/",
 "method":"get",
 "url":"http://localhost:3000/shoppinglists"}'

2 / axios-mock-adapter如何处理请求

function handleRequest(mockAdapter, resolve, reject, config) {
  if (config.baseURL && config.url.substr(0, config.baseURL.length) === config.baseURL) {
     config.url = config.url.slice(config.baseURL ? config.baseURL.length : 0);
  }
  ...
}

它与axils-mock-adapter(在测试中使用)有关,我必须使用传递的url(而不是baseURL + url)

在我的api / index.js

  axios.defaults.baseURL = 'http://localhost:3000/'

  fetchShoppingLists: () => {
    return axios.get('shoppinglists')
     .catch(error => {
       throw error
     })
  },

并在我的测试spec.js

mock.onGet('shoppinglists').reply(() => {
    return new Promise((resolve, reject) => {