首页 > 关于Pro AngularJS这本书上的一个例子!紧急求助!!!!

关于Pro AngularJS这本书上的一个例子!紧急求助!!!!

这本书首先让让我在localhost:5000上启动了一个服务,然后又在localhost:5500上启动了一个存储数据的服务器,最后在localhost:5000下的页面中用请求5500上的数据,可是这尼玛分明跨域了啊,然后书里面没有给出任何提示和讲解,作者有病吗?我怕我会砍死他!!!

最后,请求大家教教我怎么解决?

angular.module('sportsStrore').constant("dataUrl", "http://localhost:5500/products").controller('sportsStoreCtrl', function($scope, $http, dadaUrl){
    $scope.data = {};
    
    $http.get(dataUrl).success(function(data){
        $scope.data.product = data;
    });

});

你们让我加的那个头我不知道怎么加上去,因为我那个被请求的服务器是这样在终端启动的,

dpd create sportsstore
dpd -p 5500 sportsstore/app.dpd
dashboard

就这样,被请求的服务就会弹出管理页面,加哪里呢?


var allowCrossDomain = function(req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header('Access-Control-Allow-Headers', 'Content-Type');

    next();
}

在服务端的header 中添加如上字段


去npm上看看这个模块cors,cors


改成这样试试:

angular.module('sportsStrore').constant("dataUrl", "http://localhost:5500/products").controller('sportsStoreCtrl', function($scope, $http, dadaUrl){
    $scope.data = {};
    
    $http.get(dataUrl, {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    }).success(function(data){
        $scope.data.product = data;
    });

});
【热门文章】
【热门文章】