美文网首页
axios POST传值

axios POST传值

作者: AMONTOP | 来源:发表于2019-04-16 10:54 被阅读0次
    $ npm install axios
    

    GET请求:

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

    main.js:
    POST请求先全局配置后使用

    import Axios from 'axios'
    import qs from 'qs'
    
    // 全局注册,使用方法为:this.$axios
    Vue.prototype.$axios = Axios
    // 全局注册,使用方法为:this.qs
    Vue.prototype.qs = qs
    // Vue.prototype.HOST = 'api'
    
    Axios.defaults.baseURL = 'http://www.wwtliu.com'
    // Axios.defaults.headers.common['Authorization'] = AUTH_TOKEN
    Axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
    
    // 添加请求拦截器
    Axios.interceptors.request.use(function (config) {
      // 在发送请求之前做些什么
      // console.log('request', config)
      if (config.method === 'post') {
        config.data = qs.stringify(config.data)
      }
      return config
    }, function (error) {
      // 对请求错误做些什么
      return Promise.reject(error)
    })
    
    // 添加响应拦截器
    Axios.interceptors.response.use(function (response) {
      // 对响应数据做点什么
      // console.log('response', response)
      return response
    }, function (error) {
      // 对响应错误做点什么
      return Promise.reject(error)
    })
    

    POST请求:

    subLogin(){
              var userName = this.name;
              var userPwd = this.age;
              this.$axios.post('/user/loginUser',{
                username: userName,
                password : userPwd
              }).then((response)=>{
                var userMsg = response.data;
                switch (userMsg.backInfo){
                  case '1':
                    alert("登录成功");
                    window.sessionStorage.userMsg = JSON.stringify(userMsg);
                    this.$router.push({name:'ShopIndex'});
                    break;
                  case '2':
                    alert("登录失败");
                    break;
                }
              })
            }
    

    相关文章

      网友评论

          本文标题:axios POST传值

          本文链接:https://www.haomeiwen.com/subject/grbvwqtx.html