axios数据请求

 <div id="app">
    <div class="container">
      <div class="row">
        
        <h3> 静态请求【 模拟数据  】 </h3>
          <button type="button" class="btn btn-primary" @click = "getStatic" > getJson </button>
          <hr>
        <h3> 动态请求 【 真实接口】 </h3>
          <button type="button" class="btn btn-primary" @click = "get"> get </button>
          <button type="button" class="btn btn-primary" @click = "post"> post </button>
      </div>
    </div>
  </div>

引入axios

  <script src="https://cdn.bootcss.com/axios/0.19.0/axios.js"></script>
 /* 
    引入axios   cdn之后我们会得到一个axios的全局变量
  */
  // 以下代码是统一设置post请求的请求头
  axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
  new Vue({
    el: '#app',
    methods: {
      getStatic () {
        // 获取静态数据
        // console.log( axios(options) )
        // console.log( axios({}) ) // Promise

        // axios.get()
        // axios.post()

        axios({
          url: './mock/data/movie.json',
          method: 'GET'
        }).then( res => console.log( res ))
          .catch( error => console.log( error ))

      },
      get () {
        // axios
        //   .get('http://localhost/get.php',{
        //     params: {
        //       a: 1,
        //       b: 2
        //     }
        //   })
        //   .then( res => console.log( res ))
        //   .catch( error => console.log( error ))


          axios({
            url: 'http://localhost/get.php',
            method: 'GET',
            params: {
              a: 1,
              b: 2
            }
          }).then( res => console.log( res ))
            .catch( error => console.log( error ))

      },
      post () {
        // axios.post('http://localhost/post.php',{
        //   a: 1,
        //   b: 2
        // }).then( res => console.log( res ))
        //   .catch( error => console.log( error ))


        const params = new URLSearchParams()

        // params.append(key,value)
        params.append('a',1)
        params.append('b',2)

        axios({
          method: 'post',
          url: 'http://localhost/post.php',
          data: params
        }).then( res => console.log( res ) )
          .catch( error => console.log( error ))
      }
    }
  })

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值