Dominuskernel
Dominuskernel

Reputation: 91

I can't send new datas with $http.post in AngularJS

I have theses codes:

app.js (routers):

(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 (home):

<!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 (pizza list and form to add new pizza):

<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 (pizza with ingredients):

<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>

My problem is that when I want to add a new pizza in the pizza-list.html form, it doesn't work.

Before in click 'Add' new Pizza: Before to try to add new Pizza

After in click 'Add' new Pizza: after add new Pizza

This error is translate to English like:

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.

Upvotes: 0

Views: 116

Answers (1)

Erti-Chris Eelmaa
Erti-Chris Eelmaa

Reputation: 26268

The error has already answered your question(which you never asked). You are trying to submit data to a different host, other than your angular application is running.

In layman terms: you are running your application at localhost, but supplying data to 162.150.78.47. This is not allowed due to security restrictions.

Possible solutions:

1) Host everything on same domain(eg localhost)

2) Enable CORS on API side. I don't know what technology you use, but in C# you have to add [EnableCors] to your controller, which allows cross requesting.

3) Look into JSONP.

Upvotes: 1

Related Questions