【问题标题】:How to get attributes from OData response via AJAX?如何通过 AJAX 从 OData 响应中获取属性?
【发布时间】:2015-03-11 01:06:43
【问题描述】:

我正在开发通过 ajax 使用 OData 和 Web API 的 MVC 应用程序。我正在尝试使用 OData 过滤器属性从服务器端进行分页。这是我的控制器代码。

[RoutePrefix("OData/Products")]
    public class ProductsController : ODataController
    {
        private List<Product> products = new List<Product>
        {
            new Product() { Id = 1, Name = "Thermo King MP-3000", Price = 300, Category = "Thermo King" },
            new Product() { Id = 2, Name = "Thermo King MP-4000", Price = 500, Category = "Thermo King" },
            new Product() { Id = 3, Name = "Daikin Decos III c", Price = 200, Category = "Daikin" },
            new Product() { Id = 4, Name = "Daikin Decos III d", Price = 400, Category = "Daikin" },
            new Product() { Id = 5, Name = "Starcool RCC5", Price = 600, Category = "Starcool" },
            new Product() { Id = 6, Name = "Starcool SCC5", Price = 700, Category = "Starcool" }
        };


    [EnableQuery(PageSize=2)]
    public IQueryable<Product> Get()
    {
        return products.AsQueryable<Product>();
    }

    //[EnableQuery]
    //public SingleResult<Product> Get([FromODataUri] int id)
    //{
    //    var result = products.Where(x => x.Id.Equals(id)).AsQueryable();
    //    return SingleResult.Create<Product>(result);
    //}

    [EnableQuery]
    public Product Get([FromODataUri] int id)
    {
        return products.First(x => x.Id.Equals(id));
    }
}

这是我的 javascript 代码:

<script type="text/javascript">
    $(document).ready(function () {
        var apiUrl = "http://localhost:56963/OData/Products";

        $.getJSON(apiUrl,
                function (data) {
                    $("#div_content").html(window.JSON.stringify(data));
                }
            );

        //$.get(apiUrl,
        //function (data) {
        //    alert(data[0]);
        //    $("#div_content").html(data);
        //});
    });
</script>

OData 的响应是 JSON 结果,如:

{"@odata.context":"http://localhost:56963/OData/$metadata#Products","value":[{"Id":1,"Name":"Thermo King MP-3000","Price":300 ,"Category":"Thermo King"},{"Id":2,"Name":"Thermo King MP-4000","Price":500,"Category":"Thermo King"}],"@odata .nextLink":"http://localhost:56963/OData/Products?$skip=2"}

我试图获取“@odata.nextLink”但失败了,没有办法通过“data.@odata.nextLink”从javascript获取“odata.nextLink”。

谁能帮我解决这个问题?

【问题讨论】:

  • 另外我使用“data['@odata.nextLink']”也无法获取nextLink,会提示“当前上下文中不存在odata”。

标签: ajax asp.net-mvc asp.net-web-api odata


【解决方案1】:

将字符串解析为json后,data['@odata.nextLink']就可以工作了:

var data = '{"@odata.context":"http://localhost:56963/OData/$metadata#Products","value":[{"Id":1,"Name":"Thermo King MP-3000","Price":300,"Category":"Thermo King"},{"Id":2,"Name":"Thermo King MP-4000","Price":500,"Category":"Thermo King"}],"@odata.nextLink":"http://localhost:56963/OData/Products?$skip=2"}';
data = JSON.parse(data);
alert(data['@odata.nextLink']);

【讨论】:

  • 嗨@Fan,我如何使用@data.nextLink 对下一页数据进行第二次api调用?
猜你喜欢
  • 2019-04-09
  • 1970-01-01
  • 2011-09-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-02
  • 1970-01-01
  • 2015-07-15
相关资源
最近更新 更多