Javascript 如何编辑头访问控制以允许在浏览器中调用数据库Api?
我想使用ExpressAPI从mongodb数据库检索数据 javascriptJavascript 如何编辑头访问控制以允许在浏览器中调用数据库Api?,javascript,mongodb,Javascript,Mongodb,我想使用ExpressAPI从mongodb数据库检索数据 javascript $scope.nmr=function(){ $http.get("http://127.0.0.1:8081/Blogs") .then(function (response) {$scope.Blog = ((response.data).slice(0,8)); }); }; html 此错误出现在浏览器中,但返回的数据
$scope.nmr=function(){
$http.get("http://127.0.0.1:8081/Blogs")
.then(function (response)
{$scope.Blog = ((response.data).slice(0,8));
});
};
html
此错误出现在浏览器中,但返回的数据在控制台中
[database server][1]
已阻止跨源请求:同一源策略不允许读取位于的远程资源。(原因:CORS标题“访问控制允许原点”丢失)您需要执行以下操作:
var allowCrossDomain = function(req, res, next) {
if ('OPTIONS' == req.method) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
res.send(200);
}
else {
next();
}
};
app.use(allowCrossDomain);
这应该会有帮助
[database server][1]
var allowCrossDomain = function(req, res, next) {
if ('OPTIONS' == req.method) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
res.send(200);
}
else {
next();
}
};
app.use(allowCrossDomain);