• 首页 Home
  • 简介 About
  • 项目 Service
  • 案例 Cases
  • 新闻 News
  • 技术文章 本硕新闻 行业资讯

    [vue]使用axios作http请求 类似ajax

    发表时间:2019-04-09  热度:

    首先导入axios

    npm

    npm install axios

     

    直接插入js

    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>

    或者下载到本地,随你喜欢

     

    执行http请求

    get请求

    // 为给定 userid 创建请求

    axios.get('/member.php?userid=100')

      .then(function (response) {

        console.log(response);

      })

      .catch(function (error) {

        console.log(error);

      });

     

    // 可选地,上面的请求可以这样做

    axios.get('/member.php', {

        params: {

          userid: 100

        }

      })

      .then(function (response) {

        console.log(response);

      })

      .catch(function (error) {

        console.log(error);

      });

     

    post请求

    axios.post('/memberajaxurl', {

    username:username,action:"register_ajax"

      }) .then(function (response) {

        console.log(response);

      })

      .catch(function (error) {

        console.log(error);

      });

     

    通过向 axios 传递相关配置来创建请求

    axios({

      method: 'post',

      url: '/memberajaxurl',

      data: {

      username:username,action:"register_ajax"

      }

    }).then(response=> {

    this.username_notice=response.data;

    }).catch(error=> {

    this.username_notice=error;

    })

     

    *如果在Vue中使用无法将返回值指向this.data时,请使用箭头函数代替function。

    axios.post('/memberajaxurl', {

    username:username,action:"register_ajax"

      }) .then( response=> {

        this.response=response;

      })

      .catch(error=>{

        this.response=error;

      });

     

    文章怎么样?
    相关资讯