当前位置:   article > 正文

vue3 中传递 fromData 格式的写法_vue3 + laravel9 post怎么使用formdata传值

vue3 + laravel9 post怎么使用formdata传值

总结一下 当前request 请求 传递fromData 的写法 

在我们前端调用接口的使用中 我们会传递参数 大部分是json 格式的 但是有的时候 回事 formData 的格式的 那我们前端就需要修改一下 传递对应的东西

我直接上代码了那就

我说一下我使用Taro 框架中的写法  就是移动端 和小程序中的写法

  1. import Taro from "@tarojs/taro";
  2. import { useUserStore } from "../store";
  3. let needLoadingRequestCount = 0;
  4. // loading配置,请求次数统计
  5. function startLoading() {
  6. Taro.showLoading({ title: "加载中", mask: true });
  7. }
  8. function endLoading() {
  9. Taro.hideLoading();
  10. } // 声明一个对象用于存储请求个数
  11. function showFullScreenLoading() {
  12. if (needLoadingRequestCount === 0) {
  13. startLoading();
  14. }
  15. needLoadingRequestCount++;
  16. }
  17. function tryHideFullScreenLoading() {
  18. if (needLoadingRequestCount <= 0) return;
  19. needLoadingRequestCount--;
  20. if (needLoadingRequestCount === 0) {
  21. endLoading();
  22. }
  23. }
  24. //loading是做了多个请求同时发起的时候防止动画叠加
  25. export default function request(url, config: any = {}, needLoading = false) {
  26. const BASE_URL = "xxxx";
  27. //默认加载都带动画设置false不加载
  28. const userStore = useUserStore();
  29. needLoading && showFullScreenLoading();
  30. return new Promise((resolve, reject) => {
  31. Taro.request({
  32. url: `${BASE_URL}${url}`,
  33. method: config.type.toUpperCase() || "GET",
  34. data: config.data || {},
  35. timeout: 10000,
  36. header: {
  37. "Content-type": config.paramsFormdata || "application/json",
  38. Authorization: userStore.Token,
  39. ...config.header,
  40. },
  41. success: (result:any) => {
  42. const { statusCode, data } = result;
  43. if (statusCode === 200) {
  44. if (result?.token) {
  45. Taro.setStorageSync("TOKEN", result?.token);
  46. }
  47. resolve(data);
  48. } else if (statusCode === 401) {
  49. Taro.removeStorageSync("TOKEN");
  50. Taro.removeStorageSync("userInfo");
  51. setTimeout(() => {
  52. Taro.redirectTo({
  53. url: "/pages/login/index",
  54. });
  55. }, 500);
  56. }
  57. },
  58. fail: (error) => {
  59. Taro.showToast({
  60. title: "网络错误",
  61. icon: "error",
  62. });
  63. return error;
  64. },
  65. complete: (res) => {
  66. // tryHideFullScreenLoading();
  67. },
  68. });
  69. });
  70. }

我这里有一个判断 当我需要传递这个 formData 的格式的时候

  1. export function EmitCodeByH5(data) {
  2. return requestService(Api.code, {
  3. type: "post",
  4. data,
  5. // paramsFormdata: "application/x-www-form-urlencoded",
  6. });
  7. }

如果不传递 paramsFormdata 就默认是json 传就是formData 的格式的

我说一下在web端中的使用  我使用的是tdesign 的框架

人家封装好的request 的文件我就不写了

直接写api 的写法

  1. // 假设你已经有了一个 Api.page 的 URL
  2. const Api = {
  3. page: '你的URL地址',
  4. };
  5. // 假设 request.post 是一个支持发送 FormData 的函数
  6. export function page(data: any) {
  7. // 创建一个 FormData 实例
  8. const formData = new FormData();
  9. // 假设 data 是一个包含 robotId 等属性的对象
  10. // 遍历 data 的属性,并添加到 formData 中
  11. for (const key in data) {
  12. if (data.hasOwnProperty(key)) {
  13. formData.append(key, data[key]);
  14. }
  15. }
  16. // 发送请求,将 formData 作为请求体
  17. return request.post({
  18. url: Api.page,
  19. data: formData, // 这里传递 formData
  20. // 可能还需要设置 Content-Type 为 multipart/form-data,但通常库会自动处理这个
  21. // headers: {
  22. // 'Content-Type': 'multipart/form-data'
  23. // },
  24. // 注意:某些 HTTP 客户端库(如 axios)在发送 FormData 时会自动设置正确的 Content-Type
  25. });
  26. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/758558
推荐阅读
相关标签
  

闽ICP备14008679号