2014-11-06 24 views
1

我正在嘗試使用Olingo OData Client for JavaScript(odatajs)來讀取簡單的OData v4端點。如何使用odatajs讀取OData v4端點?

Olingo odatajs網頁說:

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

所以,我想用這段代碼閱讀的OData端點:

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

但代碼給出了這樣的錯誤:

Uncaught TypeError: undefined is not a function 

使用jquery/ajax它總是調用錯誤函數,但是您可以看到用提琴手的響應。

回答

1

這是odatajs git倉庫的測試案例,希望這是有幫助的:

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>"); 
}); 
相關問題