Fork Me On Github

API请求构建

本次使用 axios 作为网络请求底层

npm i axios

请求头

post 数据为json格式

接受json格式的数据

Content-Type: application/vnd.api+json

Accept: application/json

GET 请求

export function fetch<T>(url: string, params = {}): Promise<T> {
    return new Promise((resolve, reject) => {
        axios.get(url, {
            params,
        }).then((response) => {
            resolve(response.data)
        }).catch((err) => {
            reject(err)
        })
    })
}

POST 请求

export function post<T>(url: string, data = {}): Promise<T> {
    return new Promise((resolve, reject) => {
        axios.post(url, data)
            .then((response) => {
                resolve(response.data)
            }, (err) => {
                reject(err)
            })
    })
}

DELETE 请求

export function deleteRequest<T>(url: string): Promise<T> {
    return new Promise<T>((resolve, reject) => {
        axios.delete(url)
            .then((response) => {
                resolve(response.data)
            }, (err) => {
                reject(err)
            })
    })
}
点击查看全文
0 94 0