55 lines
1.6 KiB
JavaScript
55 lines
1.6 KiB
JavaScript
export const formHeaderPost = (url, method, params, header) => {
|
|
url = url + "?__random__=" + (new Date()).valueOf();
|
|
let formdata = new URLSearchParams();
|
|
Object.keys(params).map(key => {
|
|
formdata.append(key, params[key]);
|
|
});
|
|
return fetch(url, {
|
|
method, mode: "cors",
|
|
headers: { "Content-Type": "application/x-www-form-urlencoded", ...header },
|
|
body: formdata
|
|
}).then(res => res.json());
|
|
};
|
|
|
|
export const postFetch = (url, params) => {
|
|
url = url + "?__random__=" + (new Date()).valueOf();
|
|
return fetch(url, {
|
|
method: "POST",
|
|
mode: "cors",
|
|
headers: {
|
|
"Content-Type": "application/json"
|
|
},
|
|
body: JSON.stringify(params)
|
|
}).then(res => res.json());
|
|
};
|
|
export const headerRequestFetch = (url, method, params, header) => {
|
|
url = url + "?__random__=" + (new Date()).valueOf();
|
|
return fetch(url, {
|
|
method, mode: "cors",
|
|
headers: { "Content-Type": "application/json", ...header },
|
|
body: JSON.stringify(params)
|
|
}).then(res => res.json());
|
|
};
|
|
export const postExportFetch = (url, params) => {
|
|
url = url + "?__random__=" + (new Date()).valueOf();
|
|
return fetch(url, {
|
|
method: "POST",
|
|
mode: "cors",
|
|
headers: {
|
|
"Content-Type": "application/json"
|
|
},
|
|
body: JSON.stringify(params)
|
|
}).then(res => {
|
|
const filename = res.headers.get("Content-Disposition").split("filename=")[1];
|
|
res.blob().then(blob => {
|
|
const url = window.URL.createObjectURL(blob);
|
|
const a = document.createElement("a");
|
|
a.href = url;
|
|
a.download = decodeURI(filename);
|
|
a.click();
|
|
window.URL.revokeObjectURL(url);
|
|
});
|
|
});
|
|
};
|
|
|