我是Node.JS的新手,想要构建一个基本的API。这里的问题是每当我发出HTTP请求时都会收到此错误消息。
'Access-Control-Allow-Origin'标头出现在请求的资源上。因此,不允许原点“http://localhost:8001”访问。响应具有HTTP状态代码502.
以下是代码:
var server = https.createServer(options, function(req, res) {
res.setHeader('Access-Control-Allow-Origin', 'https://localhost:8001'); // client address
res.setHeader('Access-Control-Allow-Methods', 'POST');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With, content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
if (req.method == 'POST') {
req.on('data', function(data) {
data = data + '';
data = data.split('&');
if(data[0].slice(7) == 'id'){
var credentials = {};
credentials.username = data[1].slice(9);
credentials.password = data[2].slice(9);
api.login(credentials.username, credentials.password, function(err, response) {
if (err) {
console.log(err);
} else if (response === true) {
var token = success()
res.writeHead(200);
res.end(token);
我做错了吗?