Jacky
Jacky

Reputation: 1

How to get attributes from OData response via AJAX?

I'm working on the MVC application which using OData & Web API via ajax. I'm trying to do paging from server side by using OData filter attributes. Here is my code of Controller.

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

And here is my code of 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>

The response from OData is JSON result like:

{"@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"}

I was trying to get "@odata.nextLink" but failed, there is no way to get "odata.nextLink" by "[email protected]" from javascript.

Any one can help me to get through this?

Upvotes: 0

Views: 1321

Answers (1)

Fan Ouyang
Fan Ouyang

Reputation: 2132

After parse the string to json, data['@odata.nextLink'] can work:

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']);

Upvotes: 4

Related Questions