JSON在线解析

提问人:SOJSON用户 提问日期:2019-08-16 23:26 热度:14
问题标签 Nodejs 跨域

设置response返回head头信息,怎么设置全局的跨域?

1条回答 我来回答
soゝso| 2019-08-16 23:27

Nodejs跨域以及设置头信息代码如下

//对所有的请求生效
app.all('*', function (req, res, next) {
	//允许所有
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With');
    //允许请求的方法
    res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
    //如果是options 那证明链接成功
    if (req.method == 'OPTIONS') {
        res.send(200);
    } else {
        next();
    }
});