axios post请求小案例

post请求结构如下:

function post() {
    axios.post('url', {  // url表示请求地址  后面是请求传递的参数,是个对象,例如 id为1,姓名为YuChen的用户请求所需要的数据
        "id": 1,
        "name": "YuChen"
    }).then(response => {
        console.log('url', response.data)
    }, error => {
        console.log('错误', error.message)
    })
}
post()  // 调用 post

接口文档部分说明如下
axios post请求小案例_第1张图片
axios post请求小案例_第2张图片

1.在api.js上定义请求地址

const getDict = "请求url"
export const GetDict = function(param){
  return request({
    url: getDict,
    method: 'post',
    data: param
  });
}

2.在vue中

post(num){  // 在methods中定义 post 方法,在需要的地方调用即可
   GetDict({   
      typeId: num,
    }).then(response =>{  // 执行需要执行的方法
      for(let i in response.result){
       console.log(response.result[i].dictString)
      }
    })
    .catch(function(error){
      console.log(error)
    });
  },

在这里插入图片描述

你可能感兴趣的:(前端,post)