|
@@ -59,7 +59,7 @@ axios.interceptors.response.use(function (response) {
|
|
|
* @type {{async: (function(*, *, *, *): Promise<unknown>), xhr_send: request.xhr_send, sync: (function(*, *, *, *): any)}}
|
|
|
*/
|
|
|
const requestUtil = {
|
|
|
- xhr_send: function (xhr, method, headers, data) {
|
|
|
+ xhr_send(xhr, method, headers, data) {
|
|
|
if (headers) {
|
|
|
for (const key in headers) {
|
|
|
xhr.setRequestHeader(key, headers[key]);
|
|
@@ -67,8 +67,8 @@ const requestUtil = {
|
|
|
}
|
|
|
|
|
|
if (method.match(/^(POST|PUT)$/i)) {
|
|
|
- if (!headers || headers.indexOf("Content-Type") <= 0) {
|
|
|
- xhr.setRequestHeader('Content-Type', 'application/json');
|
|
|
+ if (!headers || !headers.hasOwnProperty("Content-Type")) {
|
|
|
+ xhr.setRequestHeader("Content-Type", "application/json");
|
|
|
}
|
|
|
xhr.send(JSON.stringify(data));
|
|
|
} else {
|
|
@@ -81,7 +81,7 @@ const requestUtil = {
|
|
|
* .then(data => console.log(data))
|
|
|
* .catch(error => console.error(error));
|
|
|
*/
|
|
|
- async: function (url, method, data, headers) {
|
|
|
+ async(url, method, data = {}, headers = {}) {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
xhr.open(method, url, true);
|
|
@@ -95,7 +95,7 @@ const requestUtil = {
|
|
|
/**
|
|
|
* 拼接 url
|
|
|
*/
|
|
|
- buildUrl: function (url, params) {
|
|
|
+ buildUrl(url, params) {
|
|
|
const urlObj = new URL(url);
|
|
|
// @ts-ignore
|
|
|
for (const key in params) {
|
|
@@ -104,17 +104,16 @@ const requestUtil = {
|
|
|
return urlObj.toString();
|
|
|
},
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* 同步请求 let a = request.sync("https://httpbin.tianyunperfect.cn/ip","GET",null,null)
|
|
|
*/
|
|
|
- sync: function (url, method, data, headers) {
|
|
|
+ sync(url, method, data = {}, headers = {}) {
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
xhr.open(method, url, false);
|
|
|
this.xhr_send(xhr, method, headers, data);
|
|
|
return JSON.parse(xhr.responseText);
|
|
|
- }
|
|
|
-}
|
|
|
+ },
|
|
|
+};
|
|
|
|
|
|
|
|
|
/**
|