0

i am learning coffee-script and vue.js and axio, i found an example from https://github.com/axios/axios as below

axios.get('/user', {
    params: {
      ID: 12345
    }
  })
  .then(function (response) {
    console.log(response);
  })); 

and in my vue file, i wrote this

<script lang="coffee">
import axios from 'axios'

export default
    props: ['author']
    data: ->
      info: null
    mounted: ->
      vm = this
      axios
        .get 'https://api.coindesk.com/v1/bpi/currentprice.json' 
        .then (resp) -> 
          vm.info = resp
</script>

my question is how to translate javascript code

      {
        params: {
          ID: 12345
        }
      }

to coffee script, to pass the argument directory without declare a new argument.

and the same as post example

axios.post('/user', {
    firstName: 'Fred',
    lastName: 'Flintstone'
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

i do not know how to pass it directory in coffee script.

i have try these, all faild

.get 'https://api.coindesk.com/v1/bpi/currentprice.json' (param: {id: 1})
.get 'https://api.coindesk.com/v1/bpi/currentprice.json' {param: {id: 1}}
.get 'https://api.coindesk.com/v1/bpi/currentprice.json' param: {id: 1}
.get 'https://api.coindesk.com/v1/bpi/currentprice.json' {id: 1}
.get 'https://api.coindesk.com/v1/bpi/currentprice.json' id: 1

thank you.

Phey
  • 11
  • 1

1 Answers1

1

well, i found out why. i lost a "," between.

and after that, i found my post request become an option request.

then i check Axios call api with GET become OPTIONS

after that, my vue file looks like

<script lang="coffee">
import axios from 'axios'
import qs from 'qs'

export default
    props: ['author']
    data: ->
      info: null
    mounted: ->
      vm = this
      axios
        .post 'http://localhost/get.request.test', qs.stringify {param: {id: 1}, name: 'phey'}
        .then (resp) -> 
          vm.info = resp
</script>

and the request looks like

POST /get.request.test HTTP/1.1
Origin: http://localhost:8080
Content-Length: 25
Accept-Language: zh-CN,zh;q=0.9
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Accept: application/json, text/plain, */*
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
Host: localhost
Referer: http://localhost:8080/load
Pragma: no-cache
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded

param%5Bid%5D=1&name=phey
Phey
  • 11
  • 1