增加一些访问api的实例脚本

This commit is contained in:
falcon 2022-12-27 09:30:59 +08:00
parent ac35c8db7b
commit 01798a02be
4 changed files with 12033 additions and 0 deletions

View File

@ -0,0 +1,5 @@
var serviceOption={
"url": "http://localhost:1388",
};
console.log("config.js OK!");

View File

@ -0,0 +1,50 @@
var myjs={
service(){
var opt = {
method: "post",
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: {},
credentials: 'omit',
mode: 'cors',
};
//HTTP错误处理
var cs = async function (r) {
if (r.status >= 200 && r.status < 300) {
return r;
}
if (r.status == 400) {
let emodel = await r.json();
if (emodel.Id) {
let msg = '<p>错误编号:' + emodel.Id + '</p><p>错误信息' + emodel.Message + '</p><p>发生时间:' + emodel.CreateDateTime + '</p>';
console.log(msg);
const error = new Error("400");
error.response = emodel;
throw error;
}
}
let msg = r.status + ":" + r.statusText;
console.log(msg);
const error = new Error(r.statusText);
error.response = r;
throw error;
};
return {
async data(url,data){
url = new URL(url,serviceOption.url).href;
opt.body = typeof data == 'object' ? JSON.stringify(data) : data;
return fetch(url, opt)
.catch(err => {
console.log(err);
return Promise.reject(err);
})
.then(r => cs(r)).then(j => {
return j.json();
});
},
}
}
};
console.log('myjs.js OK!');

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long