封装Ajax数据请求

export default function http(url, methods = "GET", params) {
    return new Promise((resolve, reject) => {
      const config = {
        headers: {
           "Authori-Zation": `Bearer ${localStorage.getItem("token")}`,
        },
      };
      if (methods == "POST") {
        config.method = "POST";
        config.body = JSON.stringify(params);
        config.headers["Content-Type"] = "application/json";
      }
      fetch(url, config)
        .then(resp => resp.json())
        .then(data => {
          resolve(data);
        })
        .catch(err => {
          reject(err);
        });
    });
  }

调用:

import https from "@/utils/https";

GET请求:

   async getHotData() {
      const { data } = await https(
        `https://v5.crmeb.net/api/product/hot?page=${this.page}&limit=8`
      );
      this.hot = data;
    },

POST请求:

   async getCollct() {
      await https("https://v5.crmeb.net/api/collect/all", "POST", {
        ids: this.ids,
        product: "product",
      });
    },

你可能感兴趣的:(服务器,前端,javascript)