You need to convert your response to JSON.  Im not sure how you would do that 
with .net.  Also, you should send the content-type as application/json in the 
response header so you don't have to call json.parse.

Sent from my iPhone

> On Oct 20, 2014, at 4:01 AM, Bogdan Ionescu <[email protected]> wrote:
> 
> I have a SPA project with Mvc and angular.
> I need to send from server an object (a tree) to an angular controller:
>     
>     angular.module('App').
>       controller('TreeMenuController', ["$scope", "$http", 
> treeMenuController]);
> 
>     function treeMenuController($scope, $http) {
>       var baseUrl = "Home/GetMetadata";
>       var params = {};
>       $http.post(baseUrl, params)
>               .then(function (data) {
>                       $scope.roleList = JSON.parse(data);
>               });
>     };
> 
> roleList must be in format:
> 
>     [
>               {
>             "roleName": "User", 
>             "roleId": "role1", 
>             "children": [
>                           { 
>                     "roleName": "subUser1", 
>                     "roleId": "role11", 
>                     "children": [] 
>                 },
>                           {
>                     "roleName": "subUser2", 
>                     "roleId": "role12", 
>                     "children": [
>                                       {
>                             "roleName": "subUser2-1", 
>                             "roleId": "role121", 
>                             "children": [
>                                                   { 
>                                     "roleName": "subUser2-1-1", 
>                                     "roleId": "role1211", 
>                                     "children": [] 
>                                 },
>                                                   { 
>                                     "roleName": "subUser2-1-2", 
>                                     "roleId": "role1212", 
>                                     "children": [] 
>                                }
>                                        ]
>                               }
>                               ]
>                       }
>                       ]
>               },
>           { 
>             "roleName": "Admin", 
>             "roleId": "role2", 
>             "children": [] 
>         },
>               { 
>             "roleName": "Guest", 
>             "roleId": "role3", 
>             "children": [] 
>         }
>       ]
> 
> the object to send is a List<TreeMenuItem>:
> 
>     public class TreeMenuItem
>       {
>               string roleName { get; set; }
>               string roleId { get; set; }
>               List<TreeMenuItem> children { get; set; }
>               public TreeMenuItem(string id, string name, List<TreeMenuItem> 
> children)
>               {
>                       this.roleId = id;
>                       this.roleName = name;
>                       this.children = children;
>               }
>       }
> 
> the Web method (in Home controller):
>     
>     [HttpPost]
>       public List<TreeMenuItem> GetMetadata()
>               {
>                       List<TreeMenuItem> itemsMenu = new List<TreeMenuItem>();
>                       TreeMenuItem dataSource = new TreeMenuItem("1", 
> "DataSources", null);
>                       itemsMenu.Add(dataSource);
>                       return itemsMenu;
>               }
> 
> The object send is not recognised on angular. JSON.parse throw an exception: 
> Unexpected token S.
> What I need to do?
> Thanks.
> 
> -- 
> 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 [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/angular.
> For more options, visit https://groups.google.com/d/optout.

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to