**API**(仅作记录)
// axios
import axios from "axios";
// content-type
axios.defaults.headers.post["Content-Type"] = "application/json";
// Request interceptor
axios.interceptors.request.use(
// add something
(config) => config,
// if error
(err) => {
throw Error(err);
},);
// Response Interceptor
axios.interceptors.response.use((response) => {
// Determine the response data
if (response) {
return response;
} else {
// if error
throw Error(response.data.msg);
}
});
// API address prefix
const prefix = "https://raw.githubusercontent.com/pk-cat/bottleData/master/data.json";
export function getMethod(config: any, params = {}) {
return axios.get(prefix, params);
}
export function postMethod(config: any, params = {}) {
return axios.post(prefix, params);
}
**how to use**
// http
import { getMethod } from "../API/http";
public componentWillMount() {
getMethod({})
.then((res) => {
console.log(res);
if (res.status === 200) {
console.log(res.data);
}
})
.catch((error) => {
throw error;
});
}