Can't fetch request under vue cli?

topic description

the following code is commonly used on the Internet for fetch.js

sources of topics and their own ideas

some problems encountered in the code with async and await requests

related codes

/ / Please paste the code text below (do not replace the code with pictures)
import {baseUrl} from". / env"

export default async (url ="", data = {}, type = "GET", method =" fetch") = > {

type = type.toUpperCase();
url = baseUrl + url;

if (type == "GET") {
    let dataStr = ""; //
    Object.keys(data).forEach(key => {
        dataStr += key + "=" + data[key] + "&";
    })

    if (dataStr !== "") {
        dataStr = dataStr.substr(0, dataStr.lastIndexOf("&"));
        url = url + "?" + dataStr;
    }
}

if (window.fetch && method == "fetch") {
    let requestConfig = {
        credentials: "include",
        method: type,
        headers: {
            "Accept": "application/json",
            "Content-Type": "application/json"
        },
        mode: "cors",
        cache: "force-cache"
    }

    if (type == "POST") {
        Object.defineProperty(requestConfig, "body", {
            value: JSON.stringify(data)
        })
    }
    
    try {
        const response = await fetch(url, requestConfig);
        const responseJson = await response.json();
        return responseJson
    } catch (error) {
        throw new Error(error)
    }
} else {
    return new Promise((resolve, reject) => {
        let requestObj;
        if (window.XMLHttpRequest) {
            requestObj = new XMLHttpRequest();
        } else {
            requestObj = new ActiveXObject;
        }

        let sendData = "";
        if (type == "POST") {
            sendData = JSON.stringify(data);
        }

        requestObj.open(type, url, true);
        requestObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        requestObj.send(sendData);

        requestObj.onreadystatechange = () => {
            if (requestObj.readyState == 4) {
                if (requestObj.status == 200) {
                    let obj = requestObj.response
                    if (typeof obj !== "object") {
                        obj = JSON.parse(obj);
                    }
                    resolve(obj)
                } else {
                    reject(requestObj)
                }
            }
        }
    })
}

}

what result do you expect? What is the error message actually seen?

the interface address connection timed out, and even the parameters could not be passed

When
options request is cors cross-domain and non-simple request is sent, the browser automatically sends preflight request (pre-check request) to TIMEOUT . It is very likely that your backend did not handle cors cross-domain properly. Correctly handle this options request
assuming that your backend is written with koa . Please install and use koa cors middleware https://github.com/koajs/cors; If the backend is not koa , you can correctly handle the options request by referring to the koa cors source code, or directly search the cors package of the framework you are using and install it.

Menu