search

Home  >  Q&A  >  body text

node.js - 用express开发的接口,ajax请求自定义设置headers的时候404

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也可以

高洛峰高洛峰2786 days ago443

reply all(0)I'll reply

No reply
  • Cancelreply