P粉3402642832023-08-19 00:12:20
$ npm install cors
var express = require('express') var cors = require('cors') var app = express() app.use(cors()) app.get('/products/:id', function (req, res, next) { res.json({msg: 'This is CORS-enabled for all origins!'}) }) app.listen(80, function () { console.log('CORS-enabled web server listening on port 80') })
更多详细信息请参阅: https://www.npmjs.com/package/cors
注意: CORS必须在运行API的服务器上启用。您不能在客户端代码中启用此功能。如果API支持CORS,则浏览器将发起请求