NodeJs 设置跨域后页面全部变成了源码在浏览器上显示

时间:2022-11-18 04:55:17

百度搜索跨域后得到

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();
});

虽然 我初学目前看不懂写的什么,但是经过测试就是这个跨域设置,导致接收的html文件被pre标签包裹。

app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Credentials","true");
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods","PUT,GET,POST,DELETE,OPTIONS");
res.header("ACCess-Control-Allow-Headers","Content-Type,username");
next();
});

更改,再次读取页面,成功返回html。