F12打开开发者工具-网络-选中某条请求选择复制为fetch
const data = {}
fetch("https://badianboke.com/api/xxx", {
"headers": {
"accept": "application/json, text/plain, */*",
"accept-language": "zh-CN,zh;q=0.9",
"cache-control": "no-cache",
"content-type": "application/json",
"pragma": "no-cache",
"sec-ch-ua": "\"Not.A/Brand\";v=\"8\", \"Chromium\";v=\"114\", \"Google Chrome\";v=\"114\"",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "\"Windows\"",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin"
},
"referrer": "xxxx",
"referrerPolicy": "strict-origin-when-cross-origin",
"body": JSON.stringify(data),
"method": "POST",
"mode": "cors",
"credentials": "include"
}).then(response => response.json()).then(res => {
console.log(res)
});
可以封装为同步函数如下
async function sendPost(data){
let txt = ''
await fetch("https://tapd.woa.com/api/basic/token/generate_token_from_array", {
"headers": {
"accept": "application/json, text/plain, */*",
"accept-language": "zh-CN,zh;q=0.9",
"cache-control": "no-cache",
"content-type": "application/json",
"pragma": "no-cache",
"sec-ch-ua": "\"Not.A/Brand\";v=\"8\", \"Chromium\";v=\"114\", \"Google Chrome\";v=\"114\"",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "\"Windows\"",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin"
},
"referrer": "xxxx",
"referrerPolicy": "strict-origin-when-cross-origin",
"body": JSON.stringify(data),
"method": "POST",
"mode": "cors",
"credentials": "include"
}).then(response => response.json()).then(res => {
txt = (res.name + '\n')
});
return txt
}
调用同步函数
async function start(){
let txt = ''
for (const key in datas){
txt = txt + await sendPost(datas[key])
}
}
return txt
}
const con = await start()
由于fetch请求是异步的,所以将其设置为同步函数,打印出请求返回的名字
发表评论 取消回复