express层
//设置跨域访问
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*"); //*表示允许的域名地址,本地则为'http://localhost'
res.header("Access-Control-Allow-Headers", "*");
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();
});
ajax层:
fetch(url, {
method,
mode: 'cors',
headers: {
'Test': '123456'
}
})
.then(response => response.json())
.catch(error => console.log(error))
设置Test接口404,删除headers可以,设置Accept也可以