0

I am confused on why my services aren't working. Here is where I am at https://jsfiddle.net/regy3cps/ There is some commented code on there that had an app module of ngResource to make it work along with a js file but maybe someone can tell me why that doesn't work either if they understand ngResource.

angular.module('priceCompareApp', [])

//.factory('dish', function($resource, $http){
//    var baseUrl = "http://www.decentrix.net/services/";
//    var programming = "programming/";
//
//    //return $resource(baseUrl + 'packages', {}, {
//    //    packages: {method: 'GET', isArray:true},
//    //    smartpack: {url: baseUrl + programming + 'SmartPack', method: 'GET', isArray:true},
//    //    at120: {url: baseUrl + programming + 'at120', method: 'GET', isArray:true},
//    //    at120p: {url: baseUrl + programming + 'at120p', method: 'GET', isArray:true},
//    //    at200: {url: baseUrl + programming + 'at200', method: 'GET', isArray:true},
//    //    at250: {url: baseUrl + programming + 'at250', method: 'GET', isArray:true}
//    //})
//})
//
//.controller('compareApp', function ($scope, dish){
//    $scope.packages = dish.packages;
//    $scope.smartpack = dish.smartpack();
//    $scope.at120 = dish.at120();
//    $scope.at120p = dish.at120p();
//    $scope.at200 = dish.at200();
//    $scope.at250 = dish.at250();
//});

.factory('dish', ['$http', function($http) {
    return $http.get('http://www.decentrix.net/services/packages')
        .success(function(data) {
            return data;
        })
        .error(function(err) {
            return err;
        });
}])
.controller('MainController', ['$scope', 'dish', function($scope, dish) {
    dish.success(function(data) {
        $scope.packages = data;
    });
}]);

EDIT There is something off about my code somewhere. I made my $scope.packages = manual data and it's not working.

2
  • can't have 2 success for one $http....but you can have as many then as you want. Return from each then gets sent to next then. return from success does nothing Commented Jul 22, 2015 at 4:39
  • also that url is not CORS enabled Commented Jul 22, 2015 at 4:42

2 Answers 2

1

Your Angular code looked a little off. I created another JSFiddle for you and updated the code as follows.

https://jsfiddle.net/bgerhards/xukucsuk/

var app = angular.module('myApp', [])

    .filter('html_render', ['$sce', function ($sce) {
    return function (text) {
        return $sce.trustAsHtml(text);
    };
}])


.factory('dish', ['$http', function($http) {
    return $http.get('http://www.decentrix.net/services/')
        .success(function(data) {
            return data;
        })
        .error(function(err) {
            return err;
        });
}])
.controller('MainController', ['$scope', 'dish', function($scope, dish) {
    dish.success(function(data) {
        $scope.packages = data;
    });
}]);

The body tag can be found under 'Fiddle Options'. Fiddler also seems to not have a port open for an AJAX call, so your code does not run as you may expect it to.

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

7 Comments

Can you tell me what error you are receiving? You are likely running into the same issue. You can't make an AJAX call on Localhost. Are you running the file directly? (opening the file in the browser not through a server like Apache) If you are double clicking the file, install an Apache server on your localhost and try again stackoverflow.com/questions/23100239/…
I am running a tomcat server. Running on localhost:8080 with Apache 7.0.5.7. Is this an auth issue?
Add a console.log() into your function locally and test it out. If you are not receiving any errors or the console.log(), then you are not hitting the function.
I just realized what a lot of my problem was. I had the ng-app directive on an html tag that was later not being output to the browser. Now I put it on the body tag. Also I am now getting a No 'Access-Control-Allow-Origin'
I'm glad you found your issue. I would recommend asking an additional question to the community if stackoverflow.com/questions/10143093/… does not help you correct this 'new to you' issue.
|
1

If you use the javascript developer console (F12) in Google Chrome, you will see why your resource could not be loaded

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin localhost://x.x.x.x is therefore not allowed access.

For development purposes only: Create a new shortcut to Google Chrome and in the Target add additional arguments to allow CORS and reading local json files for posterity. You need to close all Google Chrome windows and reopen using the new shortcut.

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --allow-file-access-from-files --disable-web-security

HTML

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js"></script>
    <script type="text/javascript" src="app.js"></script>
</head>
<body ng-app="priceCompareApp">
    <div ng-controller="MainController">
        <div class="row">
            <div ng-repeat="package in packages">
                <p>{{packages.name}}</p>
                <p>{{package.description}}</p>
                <p>{{package.variation}}</p>
                <p>{{package.packageId}}</p>
                <p>{{package.corePackage}}</p>
            </div>
        </div>
    </div>
</body>
</html>

app.js

(function () {

    var app = angular.module('priceCompareApp', [])

        //.factory('dish', function($resource, $http){
        //    var baseUrl = "http://www.decentrix.net/services/";
        //    var programming = "programming/";
        //
        //    //return $resource(baseUrl + 'packages', {}, {
        //    //    packages: {method: 'GET', isArray:true},
        //    //    smartpack: {url: baseUrl + programming + 'SmartPack', method: 'GET', isArray:true},
        //    //    at120: {url: baseUrl + programming + 'at120', method: 'GET', isArray:true},
        //    //    at120p: {url: baseUrl + programming + 'at120p', method: 'GET', isArray:true},
        //    //    at200: {url: baseUrl + programming + 'at200', method: 'GET', isArray:true},
        //    //    at250: {url: baseUrl + programming + 'at250', method: 'GET', isArray:true}
        //    //})
        //})
        //
        //.controller('compareApp', function ($scope, dish){
        //    $scope.packages = dish.packages;
        //    $scope.smartpack = dish.smartpack();
        //    $scope.at120 = dish.at120();
        //    $scope.at120p = dish.at120p();
        //    $scope.at200 = dish.at200();
        //    $scope.at250 = dish.at250();
        //});

        .factory('dish', ['$http', function ($http) {
            return $http.get('http://www.decentrix.net/services/packages')
                .success(function (data) {
                    return data;
                })
                .error(function (err) {
                    return err;
                });
        }])
        .controller('MainController', ['$scope', 'dish', function ($scope, dish) {
            dish.success(function (data) {
                $scope.packages = data;
            });
        }]);

})();

Comments

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.