记录使用axios进行的请求(post请求-json、表单、file,get请求)

// 用于各种的请求
// 常用发起的请求 
// get请求 1.有参 2.无参
// post请求 1.json 2.form 3.file

import axios from 'axios';
import qs from 'qs'

let baseUrl = "http://127.0.0.1:8090/sunAdmin/"
// let baseUrl = "http://192.168.50.48:8090/sunAdmin/"

// post json
// baseUrl可传,如果不传则使用默认
function postJson(addressUrl, url, data) {if (addressUrl == null || addressUrl.length > 0) {
        addressUrl = baseUrl;
    }
    let headers = { "Content-type": "application/json" }
    let JsonRequest2 = axios.create({
        baseURL: addressUrl,
        headers: headers
    })
    JsonRequest2.interceptors.response.use(
        response => {
            // 如果请求成功,直接返回响应
            return response;
        },
        error => {
            console.log("请求错误了");
            // 获取当前URL的根路径
            const baseURL = `${window.location.protocol}//${window.location.host}/`;
            // 跳转到根路径
            window.location.href = baseURL;
        })
    return JsonRequest2({
        method: 'post',
        url: url,
        data: JSON.stringify(data)
    })
}

// post form 
// baseUrl可传,如果不传则使用默认
function postForm(addressUrl, url, data) {if (addressUrl == null || addressUrl.length > 0) {
        addressUrl = baseUrl;
    }
    let headers = { "Content-type": "application/x-www-form-urlencoded" }
    let formRequest = axios.create({
        baseURL: addressUrl,
        headers: headers
    })
    formRequest.interceptors.response.use(
        response => {
            // 如果请求成功,直接返回响应
            return response;
        },
        error => {
            console.log("请求错误了");
            // 获取当前URL的根路径
            const baseURL = `${window.location.protocol}//${window.location.host}/`;
            // 跳转到根路径
            window.location.href = baseURL;
        })
    return formRequest({
        method: 'post',
        url: url,
        data: qs.stringify(data)
    })
}

// post 发送文件
function postFile(addressUrl, url, data) {if (addressUrl == null || addressUrl.length > 0) {
        addressUrl = baseUrl;
    }
    let headers = { "Content-type": "multipart/form-data;charset=utf-8" }
    let fileRequest = axios.create({
        baseURL: addressUrl,
        headers: headers
    })
    fileRequest.interceptors.response.use(
        response => {
            // 如果请求成功,直接返回响应
            return response;
        },
        error => {
            // 获取当前URL的根路径
            const baseURL = `${window.location.protocol}//${window.location.host}/`;
            // 跳转到根路径
            window.location.href = baseURL;
        })
    return fileRequest({
        method: 'post',
        url: url,
        data: data
    })
}


// get
function httpGet(addressUrl, url) {if (addressUrl == null || addressUrl.length > 0) {
        addressUrl = baseUrl;
    }
    let headers = {}
    let getRequest = axios.create({
        baseURL: addressUrl,
        headers: headers
    })
    getRequest.interceptors.response.use(
        response => {
            // 如果请求成功,直接返回响应
            return response;
        },
        error => {
            // 获取当前URL的根路径
            const baseURL = `${window.location.protocol}//${window.location.host}/`;
            // 跳转到根路径
            window.location.href = baseURL;
        })
    return getRequest({
        method: 'get',
        url: url
    })
}

export { postJson, postForm, postFile, httpGet }

以上是关于使用axios发起post请求,请求内容包括 json 表单 文件,以及get请求。

posted on 2024-08-13 02:09  李华超  阅读(3)  评论(0编辑  收藏  举报

导航