Vue发送请求

1、发送 POST 请求
//poxt请求
sendRequest() {
    const url = '请求地址';
    this.$http.post(url, {}).then(res => {
        console.log(res);
    });
}
2、发送 GET 请求
//get请求
sendRequest2() {
    const url = '请求地址'
    this.$http.get(url).then(res => {
        console.log(res)
    })
},
3、带请求头的请求
sendRequest() {
    //带请求头请求
    const url = "请求地址";
    this.$http
        .post(
        url,
        {},
        {
            headers: {
                'Content-Type':'application/x-www-form-urlencoded'
            },
        }
    )
        .then((res) => {
        console.log(res);
    });
},
4、文件下载请求
4.1 使用 POST 请求
//导出表格
export function exportExcel(data) {
  //创建form元素
  const form = document.createElement('form');
  form.method = 'post';

  //设置请求路径,可携带请求参数
  form.action = '请求地址?xxx=xxx'
  document.body.appendChild(form);
    
  //发送请求
  form.submit();
  form.remove();
}
4.2 使用 GET 请求
//导出表格
export function exportExcel(data) {
    //参数处理
    var params = 'xxxxxx';
    const url = "请求路径" + params;
    
    //定义超链接元素
    const a = document.createElement('a')
    a.href = url
    document.body.appendChild(a)
    //发送请求
    a.click()
    a.remove()
}
Logo

前往低代码交流专区

更多推荐