1.在app.js中加入下列代码,需要注意这段代码必须在router之前

// Setting up cross - domain access must be before router
app.use(function (req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header('Access-Control-Allow-Headers', 'Content-Type');
    res.header('Access-Control-Allow-Credentials', 'true');
    next();
});

示例

node.js学习笔记——跨域设置

相关文章: