通常,fetch请求需要使用无缓存控制(no-cache)来避免被浏览器缓存。可以使用以下代码来实现:
fetch(url, { cache: 'no-cache' }) .then(response => { // handle response }) .catch(error => { // handle error });
如果在开发中遇到CORS错误,可以使用代理服务器或CORS插件来解决。另外,也可以在服务器端设置CORS策略,以允许跨域请求。例如,使用Express / Node.js可以使用以下代码来设置CORS策略:
const express = require('express'); const app = express();
// Allow cross-origin requests app.use((req, res, next) => { res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE'); res.header('Access-Control-Allow-Headers', 'Content-Type'); next(); });
// Handle routes and requests app.get('/', (req, res) => { // handle request });
app.listen(3000, () => { console.log('Server started on http://localhost:3000'); });