Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何编辑头访问控制以允许在浏览器中调用数据库Api?_Javascript_Mongodb - Fatal编程技术网

Javascript 如何编辑头访问控制以允许在浏览器中调用数据库Api?

Javascript 如何编辑头访问控制以允许在浏览器中调用数据库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 此错误出现在浏览器中,但返回的数据

我想使用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

此错误出现在浏览器中,但返回的数据在控制台中

        [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);