美文网首页
vuejs发起http请求

vuejs发起http请求

作者: X_JX | 来源:发表于2017-09-20 11:33 被阅读302次

    配置文件:

    import axios from 'axios'
    import qs from 'qs'
    import * as _ from './whole'
    axios.defaults.timeout = 5000;
    axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
    axios.defaults.baseURL = 'http://www.guinaben.com:8070';
    // axios.defaults.baseURL = 'http://192.168.1.129:8383';
    //POST传参序列化
    axios.interceptors.request.use((config) => {
      if(config.method  === 'post'){
        config.data = qs.stringify(config.data);
      }
      return config;
    },(error) =>{
       _.toast("错误的传参");
      return Promise.reject(error);
    });
    //code状态码200判断
    axios.interceptors.response.use((res) =>{
      if(res.data.code != '200'){
        _.toast(res.data.msg);
        return Promise.reject(res);
      }
      return res;
    }, (error) => {
      _.toast("网络异常");
      return Promise.reject(error);
    });
    export default axios;
    

    请求:

    import axios from 'config/http'
    axios({
      method:'get',
      url: 'xxxx/xxxxx',
      params: {
        "textbook_id":id,
        "token":token
      }
    })
    .then((response) => {
      resolve(response);
    })
    axios({
      method:'post',
      url: 'teacher/pwd/resetByMobile',
      data: {
       "textbook_id":id,
        "token":token
      }
    })
    .then((response) => {
        resolve(response);
    })
    

    相关文章

      网友评论

          本文标题:vuejs发起http请求

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