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.

https://i.sstatic.net/7gMEG.png

https://i.sstatic.net/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

2
  • where are the scripts added. Please check if you imported same scripts twice. example angular.min.js and angular.js Commented Dec 17, 2013 at 11:56
  • I add the JS at the end of the file. And I have checked what you told me... but I have not imported twice the same script. Commented Dec 17, 2013 at 13:24

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. I was so worried about this problem... you have helped me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.