1

My problem is that every function in my JS, is called twice. I have been reading many threads with the same problem, but I am not able to find an appropiate answer.

Here is my code:

HTML

<!doctype html>
<html ng-app>
    <head>
        <title>News</title>

    </head>

    <body ng-controller="newsCtrl">
      <div>
        <ul>
         <li ng-repeat="item in news">
            <h2>
               {{item.shortFeed}}
                <li class="dropdown pull-right"> 
                  <a href="#" class="dropdown-toggle" data-toggle="dropdown">Click!</a>
                   <ul class="dropdown-menu pull-right">
                      <li><a ng-click="rejectNewsItem(item)">Rechazar</a></li>
                    </ul>
                 </li>
             </h2>
         </li>
        </ul>
       </div>
    </body>

    <script src="js/angular.js"></script>
    <script type="text/javascript" src="js/jquery-2.0.3.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.js"></script>
    <script src="js/pruebaRest.js"></script>

</html>

JS

function newsCtrl($scope, $http) {
    $http.get('http://localhost:49643/news/todaynews')
        .success(function (data) {
            $scope.news = data;
        })
        .error(function (data) { });

   $scope.rejectNewsItem = function (item) {
        $http.get('http://localhost:49643/news/RejectNewsItem', { params: { newsItemId: item.id } })
        .success(function (data, status, headers) {
            item.approved = false;
            item.rejected = true;
            item.selected = false;
        })
        .error(function (data) {
            alert("error");
        });
    }

}

This is the whole JS I have created. I have no .config(function ($provider)) or anything else.

Here they are some snapshots of the traffic.

http://i.stack.imgur.com/7gMEG.png

http://i.stack.imgur.com/W175T.png

The one thing I want to achieve is to make an $http request the first time the page loads, so that the page fills automatically with the information taken from the server. Also, when I click a button, to call my server and remove the item from the server. But I can't afford every call is make twice.

Thank you in advance

User 23
  • 143
  • 12

1 Answers1

1

The two screenshots that you have included do not indicate that each call is made twice. As you can see, the first call is using the HTTP verb OPTIONS. This is calling to see if the GET verb is available on the server before calling the GET. OPTIONS is a very fast call.

This can happen when you are making a CORS request with angular.

This SO question might help to answer why angular does this and when.

Hope this helps.

Community
  • 1
  • 1
Davin Tryon
  • 62,665
  • 13
  • 135
  • 126