1

I'm very new to AngularJS, and have tried to find an answer to my following question around the web.

So my issue is as follows: I have a handler (.ashx) file on my site. The handler returns a JSON array. It's located on the following URL: /services/returnsites.ashx

It's a quite simple JSON aray that looks like this:

[{"NodeId":a93064,"NodeName":"Planes"},{"NodeId":a49856,"NodeName":"Bicycles"},{"NodeId":a19631,"NodeName":"Cars"}]

I'd like to loop through each of the sites in the json array.

I have an html-file containing this:

<div ng-controller="My.Controller">
    <ul>
        <li ng-repeat="site in sites">
            {{site.NodeName}} - {{site.NodeId}}
        </li>
    </ul>
</div>

I have a controller as well, looks like this:

angular.module("umbraco").controller("My.Controller", function($scope) {
   ...
});

What I need is to put something instead of the trhee dots to grab the array from the json file.

I can get it working if I my controller looks like this (containing the array the controller file):

angular.module("umbraco").controller("My.Controller", function($scope) {
    $scope.sites = [
    {
        NodeName: "Planes",
        NodeId: "a93064"
    },
    {
        NodeName: "Bicycles",
        NodeId: "a49856"
    },
    {
        NodeName: "Cars",
        NodeId: "a19631"
    }
    ];
});

But I can't get it working if I'm trying to grab the array from the file on the server. I've tried a couple of different things including something like the $hhtp.get() etc. copied from around the web, but nothing seems to work :(

I hope that someone can help me out.

Thank you very much in advance.

/Kim

3
  • Could you post the response headers of your web service output? It's possible you've missed out some CORS headers to allow JavaScript to make a request. $http certainly seems to be one way to do it in this case, so possibly it's a CORS issue.. Do you see any errors in the JS console if you use a simple $http.get() call? Commented Nov 13, 2014 at 19:51
  • Ohh, sorry cgwyllie, didn't even see your comment before I saw the correct answer below. But thanks for yout time anyways. Commented Nov 13, 2014 at 20:07
  • No problem, glad you got it working. Commented Nov 13, 2014 at 20:13

2 Answers 2

2

First of all, your backend's JSON response is not well formatted. NodeId values have to be string, as they contain non-numeric characters.

[{
    "NodeId": "a93064",
    "NodeName": "Planes"
}, {
    "NodeId": "a49856",
    "NodeName": "Bicycles"
}, {
    "NodeId": "a19631",
    "NodeName": "Cars"
}]

After fixing that, you just need to use $http get method to make backend request.

.controller('My.Controller', function($scope, $http) {
    $http.get('returnsites.ashx').success(function(sites) {
      $scope.sites = sites;
    });
});

Here is a working plunker.

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

Comments

1

An $http.get() should do the trick. Try:

$scope.sites = [];

$http.get('/services/returnsites.ashx').success(function(data) {
    $scope.sites = data;
}); 

2 Comments

Thank you for your answer Mike. Halib get's the correct answer as he aswered first. But thanks to you as well for your time.
np, he must have answered as I was typing :)

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.