Service runs twice

79 views
Skip to first unread message

mark goldin

unread,
Sep 12, 2014, 10:16:38 AM9/12/14
to ang...@googlegroups.com
Service:
var myModule = angular.module('motoAdsApp', []);
myModule.factory("AirportService", function ($http, $q) {
    return {
        getData : function(dateFrom, dateTo){
            var deferred = $q.defer();
            var response = $http({
                    method: "get",
                    crossDomain: true,
                    dataType:"xml",
                    contentType: "application/xml",
                    url: "http://localhost/SomePage?parameters
                });
            response.success(function (data) {
                deferred.resolve(data);
            });
            response.error(function (data) {
                alert('Error');
            });
            // Return the promise to the controller
            return deferred.promise;
        }
    }
});

Controller:
var motoAdsApp = angular.module("motoAdsApp");
motoAdsApp.controller('AirportControllers', function($scope, $http, AirportService) {
    var result = AirportService.getData('09/07/2014', '09/14/2014').then(function(data) {
var json = $.xml2json(data);
  $scope.airports = json.testData;
  });

.......

The server gets hit twice when I load the page. Any idea why?

Thanks

Eric Eslinger

unread,
Sep 12, 2014, 10:52:10 AM9/12/14
to ang...@googlegroups.com
Is one of the two hits an OPTIONS request? If so, that's the CORS preflight.

e

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

mark goldin

unread,
Sep 12, 2014, 10:53:50 AM9/12/14
to ang...@googlegroups.com
In the Console it says:
XHR finished loading: GET ......
XHR finished loading: GET ......

Two identical lines.

Eric Eslinger

unread,
Sep 12, 2014, 11:34:43 AM9/12/14
to ang...@googlegroups.com
I had problems like that when I attached the same controller to multiple viewports on the same page (using ui-router), so I'm not sure if that's the thing. Where'd you put the console.log command to output that info? Have you confirmed on the server side, or in the network tab of the debug tools that the $http service is getting hit twice for certain?

e

mark goldin

unread,
Sep 12, 2014, 12:16:29 PM9/12/14
to ang...@googlegroups.com
The server gets hit twice.

Michael Hopper

unread,
Sep 12, 2014, 1:11:02 PM9/12/14
to ang...@googlegroups.com
Try adding some $log.info() or console.log() statements in various places to see what is going on. Add log statements to the beginning of the controller to see if the controller gets created twice. Also add log statements to AirportService (above the return) and insideAirportReturn.getData function.

mark goldin

unread,
Sep 12, 2014, 1:23:56 PM9/12/14
to ang...@googlegroups.com
I think I found the problem. I had ng-controller specified multiple times in the same page as it was suggested. Moved it to the top, no more multiple hits.

--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/r9mM2nIpXzk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages