S
S
Spaceoddity2020-01-12 13:30:30
JavaScript
Spaceoddity, 2020-01-12 13:30:30

How to send default data via axios?

Create with default url

Vue.use({
    install (Vue) {
    Vue.prototype.$api = axios.create({
      baseURL: 'http://127.0.0.1'
    })
  }
})

Then we call:
this.$api.post('', {
 "jsonrpc": "2.0",
 "id": 1,
 "method": "login"
})

"jsonrpc": "2.0" and "id": 1 are repeated on every call. How not to write them in the code every time?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ihor Bratukh, 2020-01-12
@BRAGA96

axios interceptors

S
Shohruh Shaimardonov, 2020-01-12
@joeberetta

const defaultData = {
  "jsonrpc":2.0,
  "id":1
}
// .......
this.$api.post('', Object.assign(defaultValues, {
 "method": "login"
}));

Ps Possible blunders, I'm writing from a mobile

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question