Ajax错误处理
控制台报的错误是:
Access to XMLHttpRequest at 'http://localhost:3000/error' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. GET http://localhost:3000/error net::ERR_FAILED
而不是400状态码
所以我们需要在app.js文件中加入
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();
});
app.get('/error', (req, res) => {
res.status(400).send('not ok');
})
这样就解决了这个问题

浙公网安备 33010602011771号