CORS解决跨域请求

跨域资源共享,应该算是现在最为推荐的跨域处理方案.

const express = require('express');
const app = express();

//设置跨域访问
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By",' 3.2.1')
res.header("Content-Type", "application/json;charset=utf-8");
next();
});


app.get('/index', (req, res) => {
res.send(index)
});
app.get('/service', (req, res) => {
res.send(service)
});

app.listen(3000, err => {
if (!err) console.log('服务器启动成功~');
else console.log(err);
});

猜你喜欢

转载自www.cnblogs.com/haoning/p/10537981.html