0

I have an Web Api controller for access data from server:

public class ServicesController : ApiController
{
    [AcceptVerbs("POST")]
    public IList<TreeMenuItem> LoadMetadata()
    {
        List<TreeMenuItem> itemsMenu = new List<TreeMenuItem>();
        TreeMenuItem dataSource = new TreeMenuItem("1", "DataSources", null);
        itemsMenu.Add(dataSource);
        return itemsMenu;
    }
}

which is call by an angularJS controler:

angular.module('App').
controller('TreeMenuController', ["$scope", "$http", treeMenuController]);

function treeMenuController($scope, $http) {
var baseUrl = "api/Services/LoadMetadata";
$http.post(baseUrl)
    .then(function (result) {
        $scope.roleList = result.data;
    });

};

In browser network I have:

Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip,deflate
Content-Length:2
Content-Type:application/json;charset=UTF-8

Request Payload {}

Response Headers:

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Content-Length: 4

in Response tab: [{}].

What is wrong?

8
  • Are you sure it's result.data and not just result? Commented Oct 20, 2014 at 12:29
  • I tried both, look at Response Tab form network browser : this what I get : {{}] Commented Oct 20, 2014 at 12:32
  • 1
    Did you put a break point in the web API - does it reaches there? Do you have another metohd name LoadMetadata? Commented Oct 20, 2014 at 12:47
  • why not use [HttpPost] rather than AcceptVerbs. also why are you not using [HttpGet]?? Commented Oct 20, 2014 at 12:55
  • The method LoadMetada is executed corectly. Commented Oct 20, 2014 at 15:40

1 Answer 1

1

I make it work: The big help was the response message when I put in browser the address for accessing api services (api/Services/LoadMetadata): The error answer was in an xml file where I found that was problem with serialization of the object TreeMenuItem. The advice was to decorate with DataContract the class and DataMember the class properties - like in WCF. After I did that (was need to add reference in project to System.Runtime.Serialization), everything was perfect.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.