params和data的差别,doc下载

 params和data的差别

export function downFile(url, parameter, method) {
  return axios({
    url: url,
    params: parameter,
    method: method ? method : "get",
    responseType: "blob",
  });
}

//  params: parameter,请求的参数,会作为查询字符串附加到 URL 上

export function downFile2(url, parameter, method) {
  return axios({
    url: url,
    method: method,
    data: parameter, // 确保请求体包含参数
    responseType: "blob", // 指定响应类型为 blob
  });
}

//data: parameter,请求体包含的参数。这通常用于发送复杂的数据,例如在 POST 请求中发送 JSON 数据。

data: parameterparams: parameter 在 Axios 请求中有不同的用途和行为,它们的主要区别如下:

使用场景示例

  • GET 请求使用 params

  • 用于发送 URL 查询字符串参数,通常用于 GET 请求。
  • 适合发送简单的键值对数据,作为 URL 查询参数的一部分。
  •  
    axios({
      url: '/api/example',
      method: 'get',
      params: {
        key1: 'value1',
        key2: 'value2'
      }
    });
    

  • 在上述例子中,数据会作为查询参数附加到 URL 上,例如 /api/example?key1=value1&key2=value2

  • POST 请求使用 data

  • 用于发送请求体中的数据,通常用于 POSTPUTPATCH 等方法。
  • 适合发送较大或较复杂的数据,如 JSON 对象、表单数据等
  • axios({
      url: 'https://api.example.com/data',
      method: 'post',
      data: {
        name: 'John Doe',
        age: 30
      }
    }).then(response => {
      console.log(response.data);
    });
    

    综上所述,data: parameter 用于发送请求体数据,适合 POSTPUT 请求;而 params: parameter 用于发送 URL 查询字符串参数,适合 GET 请求。选择使用哪种取决于你要发送的数据类型和请求方法。

下载doc文件

export function downFile2(url, parameter, method) {
  return axios({
    url: url,
    method: method,
    data: parameter, // 确保请求体包含参数
    responseType: "blob", // 指定响应类型为 blob
  });
}

export function downloadFile2(url, fileName, parameter, method) {
  return downFile2(url, parameter, method)
    .then((response) => {
      const data = response;
      const blob = new Blob([data], { type: "application/msword" }); // 设置为 DOC 文件的 MIME 类型

      if (typeof window.navigator.msSaveBlob !== "undefined") {
        window.navigator.msSaveBlob(blob, fileName);
      } else {
        const url = window.URL.createObjectURL(blob);
        const link = document.createElement("a");
        link.style.display = "none";
        link.href = url;
        link.setAttribute("download", fileName);
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link); // 下载完成移除元素
        window.URL.revokeObjectURL(url); // 释放掉 blob 对象
      }
    })
    .catch((error) => {
      console.error("文件下载失败:", error);
    });
}

 使用

     downloadFile2(
        "/asset/assetrentcontract/downLoadTemplate",
        `${data.contractName + "备案表"}.doc`,
        assetRentemplateDTO,
        "post"
      );

以下代码中的 修改 需要传什么参数: 删除操作: 在云函数中添加以下代码: async remove(params) { const res = await db.collection('users').doc(params.id).remove(); if (res.deleted === 1) { return { code: 1, msg: '删除成功' } } else { return { code: 0, msg: '删除失败' }; } }, 在vue文件中添加以下代码: async remove(id) { const users = uniCloud.importObject('users'); const res = await users.remove({ id: id }); console.log(res); }, 修改操作: 在云函数中添加以下代码: async update(params) { const res = await db.collection('users').doc(params.id).update({ name: params.name, age: params.age * 1, sex: params.sex }); if (res.updated === 1) { return { code: 1, msg: '修改成功' } } else { return { code: 0, msg: '修改失败' }; } }, 在vue文件中添加以下代码: async update(data) { const users = uniCloud.importObject('users'); const res = await users.update({ id: data.id, name: data.name, age: data.age, sex: data.sex }); console.log(res); }, 查询操作: 在云函数中添加以下代码: async getList(params) { const res = await db.collection('users').get(); if (res.data) { return { code: 1, msg: '查询成功', data: res.data } } else { return { code: 0, msg: '查询失败' }; } }, 在vue文件中添加以下代码: async getList() { const users = uniCloud.importObject('users'); const res = await users.getList(); console.log(res); // 可以将返回的数据存放到data中,用于展示在页面上 this.dataList = res.data; }, 其中,dataList是vue组件中的data属性,用于存放查询到的数据。
06-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值