SailsJS - CORS - 'Access-Control-Allow-Origin' header 包含无效值 ''
SailsJS - CORS - The 'Access-Control-Allow-Origin' header contains the invalid value ''
我生成了一个新的 sails.js 应用程序并创建了一个测试 API,如下所示。
module.exports = {
test:function (req, res) {
return res.ok({success: true});
},
}
sails
应用程序托管在一台独立的本地计算机上,位于 192.168.3.208:1337
。
我的CORS
配置如下
module.exports.cors = {
origin: '*',
credentials: true,
};
当我从本地计算机 (192.168.3.210
) 向 API、
发出简单的 AJAX
调用时
$.ajax({
type: 'GET',
url: 'http://192.168.3.208:1337/api/test',
dataType: 'json',
success: function(data) {
console.log(data);
},
failure: function(data) {
console.log(data);
}
});
我收到以下错误
Failed to load http://192.168.3.208:1337/api/test: The
'Access-Control-Allow-Origin' header contains the invalid value ''.
Origin 'http://localhost' is therefore not allowed access.
html 页面由 Apache 在端口 80 托管。
我在这里错过了什么?
这解决了问题
module.exports.cors = {
origin: '*', // Required whitelist
credentials: true,
headers: ['Authorization', 'content-type', 'user-agent']
};
我生成了一个新的 sails.js 应用程序并创建了一个测试 API,如下所示。
module.exports = {
test:function (req, res) {
return res.ok({success: true});
},
}
sails
应用程序托管在一台独立的本地计算机上,位于 192.168.3.208:1337
。
我的CORS
配置如下
module.exports.cors = {
origin: '*',
credentials: true,
};
当我从本地计算机 (192.168.3.210
) 向 API、
AJAX
调用时
$.ajax({
type: 'GET',
url: 'http://192.168.3.208:1337/api/test',
dataType: 'json',
success: function(data) {
console.log(data);
},
failure: function(data) {
console.log(data);
}
});
我收到以下错误
Failed to load http://192.168.3.208:1337/api/test: The 'Access-Control-Allow-Origin' header contains the invalid value ''. Origin 'http://localhost' is therefore not allowed access.
html 页面由 Apache 在端口 80 托管。
我在这里错过了什么?
这解决了问题
module.exports.cors = {
origin: '*', // Required whitelist
credentials: true,
headers: ['Authorization', 'content-type', 'user-agent']
};