Search code examples
odatadatajsolingo

How to read OData v4 endpoint with odatajs?


I'm trying to read simple OData v4 endpoint with Olingo OData Client for JavaScript (odatajs).

Olingo odatajs webpage says:

You may also use the documentation and the samples from the datajs library because the features and API are similar.

So I tried to read OData endpoint with this piece of code:

odatajs.read(uri, function (data) {
    alert(JSON.stringify(data));
  }, function (err) {
    alert(JSON.stringify(err));
});

But the code gives this error:

Uncaught TypeError: undefined is not a function

With jquery/ajax it always calls error function, but you can see the response with fiddler.


Solution

  • this is an test case from odatajs git repository, hope it is helpful:

    var headers = { "Content-Type": "application/json", Accept: "application/json" };
    var request = {
        requestUri: "http://<wwww bla bla .com>/endpoints/FoodStoreDataServiceV4.svc/Foods",
        method: "GET",
        headers: headers,
        data: null
    };
    
    
    odatajs.oData.request(request, function (data, response) {
        if ((response.statusCode == '200') &&
            (response.body.indexOf('}', response.body.length - 1) == response.body.length - 1) &&
            response.headers['Content-Type'] == "application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8") {
            document.getElementById('msg').innerHTML += ("<div>odatajs V4 testing pass!</div>");
        } else {
            document.getElementById('msg').innerHTML += ("<div>odatajs V4 testing failed.</div>");
        }
    }, function (err) {
        document.getElementById('msg').innerHTML += ("<div>odatajs V4 testing failed.</div>");
    });