Ajax CORS get metod 可以很好地工作,但是当使用发布方法出现错误时:对预检请求的响应未通过访问控制检查


app.post('*', function (req, res) {
res.writeHead(200, {
"Content-Type": "text/plain",
"Access-Control-Allow-Origin": "http://localhost:63342",
"Access-Control-Allow-Credentials": "true"
});
res.send('haha');
});
app.get('*', function (req, res) {
res.writeHead(200, {
"Set-Cookie": ['name=tom'],
"Content-Type": "text/plain",
"Access-Control-Allow-Origin": "http://localhost:63342",
"Access-Control-Allow-Credentials": "true"
});
fs.readFile('info.txt', function (err, data) {
res.write(data);
res.end();
})
});

为什么用同样的方式(CORS(来做,get方法可以做得很好,但是 发布方法不能,错误:在此处输入图像描述

你试过节点模块 cors 吗?

https://www.npmjs.com/package/cors

app.use(cors());

最新更新