Ajax的请求方式

452 阅读1分钟

ajax请求方式:get 、post、delete、put、patch

语法

// 发送请求
axios({
  method: '请求方式',
  url: '服务器的路径',
  参数: {
    键: '值',
  }
});

1、发送get请求方式

get方式的参数传递有两种常见方式: 1.在url拼接: 地址?参数=值&参数=值 2.在配置项中通过params指定参数对象

// 发送get请求
axios({
  method: 'get',
  url: '接口URL',
  params: {
    id: 110,
  }
}).then(result=>{
    console.log(result);   // 如果请求成功,则返回数据result
})
​
//__________________
axios({
  method: 'get',
  url: '接口URL?id=110',
}).then(result=>{
    console.log(result);   // 如果请求成功,则返回数据result
})

2、发送post请求方式:新增,注册,登陆

post在配置项中通过data指定参数对象

// 发送post请求
axios({
  method: 'post',
  url: '接口URL',
  data: {
    id: 110,
  }
}).then(result=>{
    console.log(result);   // 如果请求成功,则返回数据result
})

delete、put、patch这三种请求方式也一样:

delete:删除

put:完整的编辑

patch:部分更新

参数对象:

1、get delete 的参数需要使用params来设置

2、put patch post 的参数需要使用data来设置