fetch(url)
console.dir(fetch);
fetch( url, { method: '', headers: { 'Content-Type': 'application/json' }, body: '' } ) .then( res => res.json() ) .then( res => { console.log( 'success ', res ) } )
根据请求的数据类型,返回的结果分别有:
fetch('https://glpla.github.io/utils/data/rank/20240203.json') .then(res => res.json()) .then(res => console.log(res))
fetch('./tmp.txt') .then(res => res.text()) .then(res => console.log(res))
fetch('../../common/drill.html') .then(res => res.text()) .then(res => { document.getElementById('require').innerHTML = res; })
fetch('https://glpla.github.io/utils/avatar/avatar0.png') .then(res => res.blob()) .then(res => img.src = URL.createObjectURL(res))
ok: true redirected: false status: 200 statusText: "OK" type: "cors" url: "http://localhost:3000/clerk/2002"
ok: false redirected: false status: 404 statusText: "Not Found" type: "cors" url: "http://localhost:3000/clerk/2002"
fetch('/utils/data/cart.json') .then(response => { if (!response.ok) { throw new Error('Network response was not ok ' + response.statusText); } return response.json(); }) .then(data => { console.log(data); }) .catch(error => { console.error('There was a problem with the fetch operation:', error); }) .finally(() => { console.log('Fetch operation completed.'); });
默认获取数据 - 一般是获取所有数据
fetch('https://glpla.github.io/utils/data/rank/20240203.json') .then(res => res.json()) .then(res => console.log(res))
使用查询参数 query,直接在 url 后面使用 ? 拼接查询值对,使用 & 拼接多个查询参数;如获取指定数据或分页获取数据
fetch('http://127.0.0.1:3000/test?id=20240507') .then(res => res.json()) .then(res => console.log(res))
使用路径参数 params
fetch('http://127.0.0.1:3000/test/20240507') .then(res => res.json()) .then(res => console.log(res))
router.get('/', (req, res) => { console.log(req.query); res.header('Access-Control-Allow-Origin', '*') res.json({ code: 'ok', type: 'get', data: req.query.id }) }) router.get('/:id', (req, res) => { console.log('params', req.params); res.header('Access-Control-Allow-Origin', '*') res.json({ code: 'ok', type: 'get', data: req.params.id }) })
传递JSON数据
fetch(url + '/cont', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ id: 20240508, }) }) .then(res => res.json()) .then(res => console.log(res))
传递值对,如表单数据
fetch('http://127.0.0.1:3000/test', { method: 'post', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: 'id=20240507' }) .then(res => res.json()) .then(res => console.log(res))
router .use(express.urlencoded({ extended: true })) // for parsing application/x-www-form-urlencoded .use(express.json()) // for parsing application/json when POST
router.post('/', (req, res) => { console.log('req body', req.body); res.json({ code: 'ok', type: 'post', data: req.body.id }) })
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
fetch('http://127.0.0.1:3000/test/20240507', { method: 'DELETE' }) .then(res => res.json()) .then(res => console.log(res))
router.delete('/:id', (req, res) => { console.log('params', req.params); res.json({ code: 'ok', type: 'delete', data: req.params.id }) })
fetch('http://127.0.0.1:3000/test/123', { method: 'put', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ id: 20240507 }) }) .then(res => res.json()) .then(res => console.log(res))
router.put('/:id', (req, res) => { console.log('params', req.params); console.log('req body', req.body); res.json({ code: 'ok', type: 'delete', data: req.params.id }) })