如何使用 Html/Javascript 使用 OData 服务?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10112376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to consume OData service with Html/Javascript?
提问by AlvinfromDiaspar
Our project currently uses Silverlight to consume an Odata service. This has made life pretty simple since we can just reference the OData service thus giving us generated service reference/entities.
我们的项目目前使用 Silverlight 来使用 Odata 服务。这让生活变得非常简单,因为我们可以只引用 OData 服务,从而为我们提供生成的服务引用/实体。
However there is some discussion on whether we should move to Html (html5). I'd like to know what to expect if we make this change. We'd be leveraging a framework like jQuery of course.
然而,有一些关于我们是否应该转向 Html (html5) 的讨论。我想知道如果我们进行此更改会发生什么。我们当然会利用像 jQuery 这样的框架。
- My main concern is how to consume the same OData service via JavaScript/jQuery.
- How are we suppose to deserialize/serialize entities returned from this OData service?
- Is our data contract supposed to be hard-coded (if so, this is really unacceptable for us)?
- 我主要关心的是如何通过 JavaScript/jQuery 使用相同的 OData 服务。
- 我们应该如何反序列化/序列化从此 OData 服务返回的实体?
- 我们的数据契约是否应该是硬编码的(如果是这样,这对我们来说真的是不可接受的)?
Thanks!
谢谢!
采纳答案by Rich Turner
OData sources can return data as JSONso your web pages can XHR your data and receive it as JSON which gets de-serialized back into a Javascript object for you to pick apart and act-upon or display.
OData 源可以将数据作为 JSON 返回,因此您的网页可以对您的数据进行 XHR 并将其作为 JSON 接收,然后将其反序列化为 Javascript 对象,以便您进行拆分和操作或显示。
Here are some additional links to get you started:
以下是一些可帮助您入门的附加链接:
- New Javascript OData Library [MSDN]
- OData protocol by example [MSDN]
- Leveraging OData end-points in JSON format with JQuery
- Consume an OData service with JayData
- Consume an OData service with BreezeJS
- 新的 Javascript OData 库 [MSDN]
- OData 协议示例 [MSDN]
- 在 JQuery 中利用 JSON 格式的 OData 端点
- 使用 JayData 使用 OData 服务
- 使用 BreezeJS 使用 OData 服务
HTH.
哈。
回答by Mark Stafford - MSFT
We have also produced a pretty cool little library called Data.js (http://datajs.codeplex.com/)that will significantly speed up OData consumption from JavaScript. Here's a sample in CoffeeScript:
我们还制作了一个非常酷的小库,称为Data.js (http://datajs.codeplex.com/),它将显着加快 JavaScript 的 OData 消耗。这是 CoffeeScript 中的一个示例:
success = (data) -> $("#searchResultsTemplate").tmpl(data).appendTo("#resultsArea")
error = (err) -> $("#resultsArea").text(JSON.stringify(err.message))
do ->
$("#search").click(->
OData.defaultHttpClient.enableJsonpCallback = true
OData.read("http://odata.netflix.com/v2/Catalog/Titles?$top=5", success, error))
And the JavaScript it generates:
以及它生成的 JavaScript:
success = function(data) {
return $("#searchResultsTemplate").tmpl(data).appendTo("#resultsArea");
};
error = function(err) {
return $("#resultsArea").text(JSON.stringify(err.message));
};
(function() {
return $("#search").click(function() {
OData.defaultHttpClient.enableJsonpCallback = true;
return OData.read("http://odata.netflix.com/v2/Catalog/Titles?$top=5", success, error);
});
})();
So far I've been successful using it with CoffeeScript, jQuery and Knockout.js.
到目前为止,我已经成功地将它与 CoffeeScript、jQuery 和 Knockout.js 一起使用。
回答by Peter Aron Zentai
As an alternative, you can give a shot to JayData, which has oData support - based on the supercool datajs library. It provides an abstract data access layer over several storage providers or protocols, one important of them is OData.
作为替代方案,您可以尝试JayData,它具有 oData 支持 - 基于 supercool datajs 库。它在多个存储提供程序或协议上提供了一个抽象的数据访问层,其中一个重要的是 OData。
The above mentioned query would look something like this
上面提到的查询看起来像这样
var source = new $data.yourOdataContext({serviceUri:"http://odata.netflix.com/v2/Catalog"});
source.Titles
.take(5)
.forEach( function(catalog) { render(catalog); });
As you might wouldn't expect this gets translated to .../Titles?$filter=5, so operations are not done on the client, even if the simple syntax might suggest.
正如您可能不会想到这会被转换为 .../Titles?$filter=5,因此操作不会在客户端上完成,即使简单的语法可能会提示。
JayData will give you JavaScript Language Query (JSLQ)letting you query for data using the ES5 standard filter function: all with JavaScript, not knowledge of OData query syntax is required.
JayData 将为您提供JavaScript 语言查询 (JSLQ),让您可以使用 ES5 标准过滤器功能查询数据:全部使用 JavaScript,无需了解 OData 查询语法。
回答by Jovan MSFT
If you want to display data in the table and use sorting, paging, search, you can use jQuery dataTables plugin https://www.datatables.net/with OData connector http://vpllan.github.io/jQuery.dataTables.oData/
如果要在表格中显示数据并使用排序、分页、搜索,可以使用 jQuery dataTables 插件https://www.datatables.net/和 OData 连接器http://vpllan.github.io/jQuery.dataTables。 o数据/
You don't need any additional programming since dataTables will perform there operations for you.
您不需要任何额外的编程,因为 dataTables 将为您执行那里的操作。
回答by Sudo.Root
You could this OData client based on axios I wrote.
你可以基于我写的 axios 这个 OData 客户端。
https://github.com/fabio-nettis/ODCJ
https://github.com/fabio-nettis/ODCJ
The OData Connector For Javascriptor more formerly know as ODCJ, is a promise based client that uses axios to establish connections to OData V3/V4 services. ODCJ provides a lot of useful functions to customize the request url and filtering properties.
该OData的连接器对JavaScript或多个以前知道的ODCJ,是一个基于客户承诺使用爱可信建立到的OData V3 / V4服务的连接。ODCJ 提供了很多有用的功能来自定义请求 url 和过滤属性。