【发布时间】:2016-01-31 10:54:32
【问题描述】:
我在通过 wamp 构建的自定义网站上经常遇到这个问题。
我按照说明如何为 ssl 生成自签名证书和私钥。然后我使用 nodejs/expressjs 创建一个 https 服务器,为我的网站上的数据使用 web api。然后我使用 angularjs 来显示数据。
现在令人困惑的是,有时它可以在 Google Chrome 上运行。但是,它在 firefox、opera 和 Microsoft Edge 上肯定会失败。
在 angularjs 文件中我有这个:
testControllers.controller('summoner-by-name', ['$scope', '$http', '$resource', function($scope, $http, $resource) {
$scope.summonerName = {text: 'abc'};
$scope.items = regions;
$scope.submit = function ()
{
setTimeout(function() {
data = {"summonerName": $scope.summonerName.text, "region": $scope.items.selectedOption.name, "PID": "000"};
$http({
method: 'POST',
url: 'https://localhost:3030/custom_Project',
dataType: "json",
headers: {
"Content-Type": "application/json",
"Content-Length": data.length
},
data: data
}).then(function(response) {
$scope.posts = response.data;
});
}, 1000);
}
}]);
...
在节点 js 文件上,我有这个:
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', 'https://custom_site.com');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
var options = {
key: fs.readFileSync('C:/wamp/www/custom_Project/ssl_certs/private.key'),
cert: fs.readFileSync('C:/wamp/www/custom_Project/ssl_certs/public.crt'),
ca: fs.readFileSync('C:/wamp/bin/php/php5.5.12/cacert.pem'),
};
app.post('/custom_Project', function (req, res) {
...
}
https.createServer(options, app).listen(3030);
以下是我在尝试从 nodejs/expressjs 服务器检索数据时在控制台上收到的消息:
微软边缘:
SCRIPT7002:XMLHttpRequest:网络错误 0x80070005,访问被拒绝。
火狐:
跨域请求被阻止:同源策略不允许读取位于https://localhost:3030/custom_Project 的远程资源。 (原因:CORS 请求失败)。
Opera(偶尔在 Chrome 上):
选项https://localhost:3030/custom_Project net::ERR_INSECURE_RESPONSE
我假设它是因为证书是自签名的,但我可能错了。有什么想法吗?
【问题讨论】:
-
更新:我决定尝试一些建议,看看是否可以解决我的问题。到目前为止,这些是我尝试过的,但它们还没有为我解决问题: 1. 我通过 npm 添加了 cors 模块; 2. 我在我的根文件夹中添加了一个 .htaccess 文件,它允许使用 * 进行访问控制允许来源; 3. 我编辑了 httpd.conf 以包含 Access-control-allow-origin;
-
我遇到了完全相同的问题。
标签: angularjs node.js express xmlhttprequest