axios 俩种方式

65 阅读1分钟

get (get,delete,head只支持params传参)


axios.get('http://localhost:3000/comment/new?type=2&id=19723756&sortType=2').then(res => {
      console.log(res.data.data);
    })

  
    axios.get('http://localhost:3000/comment/new', {
      params: {
        username:"张三"password:"123",
      },
    }).then(res => {
      console.log(res.data.data);
    })
    
    
    axios({
      url: 'http://localhost:3000/comment/new',
      method: 'get',
      params: {
       username:"张三"password:"123",
      },
      // data: 'a=1&b=2',  // application/x-www-form-urlencoded
      withCredentials: true,  // 携带cookie的
    }).then(res => {
      console.log(res);
    })

post

 传参方式一: data post 请求的数据
  axios.post('http://localhost:3000/comment/new', {
        username:"张三"password:"123",
    }).then(res => {
      console.log(res.data.data);
    })
    
   传参方式二:
   如果服务器不支持json类型的参数,支持urlencoded类型的参数,
     axios.post('http://localhost:3000/comment/new', "username="张三"&password="123",}).then(res => {
      console.log(res.data.data);
    })
    
   传参方式三:
   this.$http({
      url: 'http://localhost:3000/comment/new',
      method: 'post',
      params: this.query,
      data: {  // 默认是json格式
        a:1,
        b:2
      },
      withCredentials: true,  // 携带cookie的
    }).then(res => {
      console.log(res);
    })