我无法在AngularJS中使用$ http.post发送新数据

时间:2014-08-03 10:57:10

标签: javascript html angularjs post http-post

我有这些代码:

app.js(路由器):

(function(){    
    var pizzasApp = angular.module('pizzasApp',['ngRoute','app']);

    //here is declared the routers
    pizzasApp.config(['$routeProvider', function($routeProvider){

        $routeProvider.
        when('/pizzas',{
            templateUrl: 'pizza-list.html',
            controller: 'PizzasController'
        }).
        when('/',{
            redirectTo:'/pizzas'
        }).
        when('/pizzas/:pizzaId',{
            templateUrl: 'pizza-detail.html',
            controller: 'PizzasDetailController'
        }).
        otherwise({
            redirecTo: '/pizzas'
        });
    }]);

})();

configure.js(controllers):

(function(){

    var app = angular.module('app',[]);

    //In this controller is got all pizzas from JSON file and post new pizzas  
    app.controller('PizzasController',['$scope','$http',function($scope,$http){
        $http.get('http://162.250.78.47/api/pizzas').success(function(data){
            $scope.pizzas = data;
        });

    //WHEN CLICK THE BUTTON FORM FROM pizza-list.html THIS ADD NEW PIZZA. BUT THIS DOESN'T WORK
        $scope.save = function(){
            $http.post('http://162.250.78.47/api/pizzas',$scope.pizzas).then(function(data){
                $location.path('/pizzas');
            });
        };
    }]);

    //In this controller I get a selected pizza with ingredients
    app.controller('PizzasDetailController',['$scope','$http','$routeParams',function($scope,$http,$routeParams){
        $scope.pizzaId = $routeParams.pizzaId;

        $http.get('http://162.250.78.47/api/pizzas/'+$scope.pizzaId).success(function(data){
            $scope.pizza = data;
        });
        $http.get('http://162.250.78.47/api/ingredients').success(function(ingr){
            $scope.ingrs = ingr;
        });
    }])

})();

index.html(主页):

<!DOCTYPE html>
<html lang="en" ng-app="pizzasApp">
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" type="text/css" href="bootstrap-3.2.0/css/bootstrap.min.css">
        <link rel="stylesheet" type="text/css" href="bootstrap-3.2.0/css/bootstrap-theme.min.css">
        <script type="text/javascript" src="jquery-2.1.1.min.js"></script>
        <script type="text/javascript" src="bootstrap-3.2.0/js/bootstrap.min.js"></script>
        <script type="text/javascript" src="angular.min.js"></script>
        <script type="text/javascript" src="angular-route.min.js"></script>
        <script type="text/javascript" src="app.js"></script>
        <script type="text/javascript" src="configure.js"></script>
        <title>New Web Project</title>
    </head>
    <body>
        <div ng-view></div>
    </body>
</html>

pizza-list.html(披萨列表和添加新披萨的表格):

<div ng-repeat="nombre in pizzas">
    <p><a href="#/pizzas/{{nombre._id}}">{{nombre.name}}</a></p>

</div>

<!-- HERE IS THE PROBLEM -->
<form>
    <label>Write the pizza name that you want add:</label></br>
    <input type="text" ng-model="pizzas.name" placeholder="Pizza">
    <button ng-click="save()">Add</button>
</form>

pizza-detail.html(配料披萨):

<div>
    <p>{{pizza.name}}</p>   
        <div ng-repeat="ing in pizza.ingredients track by $index">
            <div ng-repeat="ingr in ingrs">
                <div ng-if="ing == ingr._id">
                    {{ingr.name}}
                </div>
            </div>
        </div>
</div>

我的问题是,当我想在pizza-list.html表单中添加新披萨时,它不起作用。

点击之前添加&#39;新披萨: Before to try to add new Pizza

点击后添加&#39;新披萨: after add new Pizza

此错误转换为英语,如:

Blocked request from various sources: the same origin policy prevents read http://162.250.78.47/api/pizzas remote resource. This can be fixed by moving the action to the same domain or activating CORS.

1 个答案:

答案 0 :(得分:1)

错误已经回答了您的问题(您从未问过)。您正尝试将数据提交到其他主机,而角色应用程序正在运行。

通俗地说:您正在localhost上运行您的应用程序,但是将数据提供给162.150.78.47。由于安全限制,这是不允许的。

可能的解决方案:

1)托管同一域上的所有内容(例如localhost)

2)在API端启用CORS。我不知道你使用什么技术,但在C#中你必须将[EnableCors]添加到你的控制器,这允许交叉请求。

3)查看JSONP。