发送网络请求

发送网络请求

在Vue.js中发送网络请求本质还是ajax,我们可以使用插件方便操作。

  • vue-resource

    Vuejs的插件,已经不维护,作者不推荐使用

  • axios

    可以在任何地方使用,推荐

axios

既可以在浏览器端又可以在node.js中使用的发送http请求的库,支持Promise,默认不支持jsonp。官网

  • 发送get请求

    axios.get('http://localhost:3000/brands')
          .then(res => {
            console.log(res.data);
          })
          .catch(err => {
            console.dir(err)
          });
    
  • 发送delete请求

    axios.delete('http://localhost:3000/brands/109')
          .then(res => {
            console.log(res.data);
          })
          .catch(err => {
            console.dir(err)
          });
    
  • 发送post请求

    axios.post('http://localhost:3000/brands', {name: '小米', date: new Date()})
          .then(res => {
            console.log(res);
          })
          .catch(err => {
            console.dir(err)
          });
    
  • jsonp

    https://github.com/axios/axios/blob/master/COOKBOOK.md

    jsonp('http://localhost:3000/brands', (err, data) => {
          if (err) {
            console.dir(err.msg);
          } else {
            console.dir(data);
          }
        });
    
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容