0

我有一个用于从服务器访问数据的 Web Api 控制器:

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;
    }
}

由 angularJS 控制器调用:

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;
    });

};

在浏览器网络中,我有:

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

请求负载 {}

响应标头:

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

在响应选项卡中:[{}]。

怎么了?

4

1 回答 1

1

我让它工作:最大的帮助是当我在浏览器中输入访问 api 服务的地址 (api/Services/LoadMetadata) 时的响应消息:错误答案在一个 xml 文件中,我发现这是对象序列化的问题树菜单项。建议是用 DataContract 类和 DataMember 类属性来装饰 - 就像在 WCF 中一样。在我这样做之后(需要在项目中添加对 System.Runtime.Serialization 的引用),一切都很完美。

于 2014-10-21T06:01:49.320 回答