Send object (C#) from server to angular $http

256 views
Skip to first unread message

Bogdan Ionescu

unread,
Oct 20, 2014, 4:01:47 AM10/20/14
to ang...@googlegroups.com
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.

Raul Vieira

unread,
Oct 20, 2014, 7:49:10 AM10/20/14
to ang...@googlegroups.com
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
--
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.

Bogdan Ionescu

unread,
Oct 20, 2014, 7:52:33 AM10/20/14
to ang...@googlegroups.com
The content-type is application/json (is default for $http).

Raul Vieira

unread,
Oct 20, 2014, 7:55:36 AM10/20/14
to ang...@googlegroups.com
Why are parsing the data param?

Sent from my iPhone

Bogdan Ionescu

unread,
Oct 20, 2014, 8:00:17 AM10/20/14
to ang...@googlegroups.com
I'm not experienced in angular and json, so I tried different versions until it's work ....

--
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/6NwbkWQHb8M/unsubscribe.
To unsubscribe from this group and all its topics, 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.



--
Bogdan Mihai Ionescu


Raul Vieira

unread,
Oct 20, 2014, 8:11:18 AM10/20/14
to ang...@googlegroups.com
Fair enough.

I'm not a .net dev but maybe this can help you http://msdn.microsoft.com/en-us/library/bb412179(v=vs.110).aspx

Sent from my iPhone

Bogdan Ionescu

unread,
Oct 20, 2014, 8:15:14 AM10/20/14
to ang...@googlegroups.com
The link is for WCF, which is not applicable in my situation (MVC web method).
Thanks anyway.

Mauro Servienti

unread,
Oct 20, 2014, 8:21:45 AM10/20/14
to ang...@googlegroups.com

  

    [HttpPost]

            public List<TreeMenuItem> GetMetadata()

                        {

                                   List<TreeMenuItem> itemsMenu = new List<TreeMenuItem>();

                                   TreeMenuItem dataSource = new TreeMenuItem("1", "DataSources", null);

                                   itemsMenu.Add(dataSource);

                                   return itemsMenu;

                        }

 

 

[.m] is the above in an MVC controller or WebAPI controller?

 

.m

Bogdan Ionescu

unread,
Oct 20, 2014, 8:26:47 AM10/20/14
to angular
It was on a Mvc controller, but now is an Api Controller . And I have the same problem.
I want to use Web api.

--
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/6NwbkWQHb8M/unsubscribe.
To unsubscribe from this group and all its topics, 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.



--
Bogdan Mihai Ionescu


Mauro Servienti

unread,
Oct 20, 2014, 8:48:10 AM10/20/14
to ang...@googlegroups.com

It was on a Mvc controller, but now is an Api Controller . And I have the same problem.

I want to use Web api.

[.m] in MVC that can’t work, full stop, you need to introduce some sort of a “JsonResult” on the server, in WebAPI it works out-of-the-box, I do that the entire day :-)

You do not need the JSON.parse on the angular side, what you get back is already a valid json response, take a look with the browser tools :-)

 

William Thiago

unread,
Oct 20, 2014, 9:23:07 AM10/20/14
to ang...@googlegroups.com
WebAPI returns json or xml in accordance with the request.
Make sure you're requesting application/json in accept header (view in fiddle, or chrome inspect, or firebug, etc).

An alternative is set WebAPI to return always as json, removing xml formater, or changing the content negotiator:

[]s
William Thiago

Bogdan Ionescu

unread,
Oct 20, 2014, 9:33:59 AM10/20/14
to angular
I did (look on network browser tools): the response is [{}]

--
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/6NwbkWQHb8M/unsubscribe.
To unsubscribe from this group and all its topics, 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.



--
Bogdan Mihai Ionescu


Bogdan Ionescu

unread,
Oct 21, 2014, 4:54:51 AM10/21/14
to ang...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages