VUE3请求意外报跨越错误或者500错误问题

发布于:2024-08-14 ⋅ 阅读:(203) ⋅ 点赞:(0)

1.有可能是请求传参和传参类型写错了

首先要确保该请求接口是支持跨域的(不支持叫后端改)

access-control-allow-headers:Content-Type, Accept, Access-Control-Allow-Origin, api_key, Authorization
access-control-allow-methods:GET, POST, OPTIONS
access-control-allow-origin:*

在这里插入图片描述

application/x-www-form-urlencoded 是一种 HTTP 请求体的 MIME 类型,它定义了如何将表单数据编码成键值对的形式。这种编码方式通常用于发送表单数据到服务器,在这种编码下,键值对通过 = 连接,不同的键值对之间通过 & 分隔。

JS原生写法

// 发送请求
fetch("http://Files.aliyinba.com/Home/PackedFile", {
 method: "POST",
 headers: {
   "Content-Type": "application/x-www-form-urlencoded",//传参类型
 },
 body: "FilePath=/SiteFiles/image/20240813/202408131135564792ss.png,/SiteFiles/image/20240813/202408131041042778ss.png",//请求传参
    })
      .then((response) => response.json())
      .then((data) => console.log(data))
      .catch((error) => console.error("Error:", error));
  }

或者使用axios

import axios from "axios";//引用axios
  
const rawData = "FilePath=" + arrayOrComma(arr);//请求参数
    axios
      .post("https://Files.aliyinba.com/Home/PackedFile", rawData, {
        headers: {
          "Content-Type": "application/x-www-form-urlencoded",//传参类型
        },
      })
      .then((response) => {
        console.log(response.data);
      })
      .catch((error) => {
        console.error("Error sending data:", error);
      });

网站公告

今日签到

点亮在社区的每一天
去签到