Steve Gaines Steve Gaines - 5 months ago 9
AngularJS Question

Why does AngularJS send double network requests?

I have the code shown below in a controller for an AngularJS Single Page Application. When this page runs and I check Developer Tools Network (in IE, Chrome, and Firefox) it always shows 2 successful GET calls to Tables, and two empty OPTIONS calls to Tables. Why are there 2 GET calls? Is that normal or did I do something wrong in my code? Also, why does it issue the 2 OPTIONS calls?



"use strict";

app.controller('AdminController', function ($scope, $http)
{
$scope.$parent.Title = "Admin";

var url = $scope.$parent.BaseUrl + "Tables";
$http.get(url)
.then(function mySuccess(response)
{
$scope.MyTables = response.data;
});
});




Answer

This has happened to me in the past, the issue was that I had defined my controller twice, once in the route:

.state('app.state', {
  url: '/state',
  controller: 'SomeCtrl',
  templateUrl: 'views/state.html'
})

and then defined it again in my view HTML:

<div ng-controller="SomeCtrl"></div>

As far as I am aware, you should only define it in one or the other.