function checkStatus(response) {
if (response.status >= 200 && response.status < 300) {
return response;
}
const error = new Error(response.statusText);
error.response = response;
throw error;
}
function parseJSON(response) {
return response.json();
}
function request(url,option) {
return fetch(url, option)
.then(checkStatus)
.then(parseJSON)
.catch((err)=>{console.log(err, `请求${url}报错`)})
// .then((res) => checkResponse(res, options))
// .catch(err => handleError(err, options));
}
function stringify(obj = {}) {
return Object.keys(obj)
.filter(k => obj[k] || +obj[k] === 0)
.map(k => {
let value = obj[k];
if (typeof value == 'object') {
value = encodeURIComponent(JSON.stringify(value));
} else {
value = encodeURIComponent(value);
}
return encodeURIComponent(k) + '=' + value;
})
.join('&');
}
//抛出post方式的请求
export function post(url, params={}){
return request(url, {
method: 'POST',
// credentials: 'same-origin',
mode:"no-cors",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: stringify(params)
});
}
//抛出get方式的请求
export function get(url, params={}){
return request(`${url}?${stringify(params)}`, {
method: 'GET',
// credentials: 'same-origin',
mode:"no-cors",
});
}
//定义普通的请求
export function getApplyPay(data){
return post('https://sla.console.aliyun.com/coupon_apply/apply.json',data)
}
//定义async awit方式的请求
export async function getInstanceDetail(data, options) {
return await get('/instance_info/list.json', data, {ignoreError: true});
}