英文:
Not able to use OData V2 (Northwind) data in SAPUI5 App
问题
我正在尝试在我的SAPUI5应用中使用Northwind数据服务(OData V2)。但是,我无法从服务器获取任何数据。
这是我的XML视图:
<mvc:View controllerName="c.g.odataapp2.controller.Root"
xmlns:mvc="sap.ui.core.mvc"
displayBlock="true"
xmlns="sap.m">
<Page id="page" title="{i18n>title}">
<Table id="idOrdersTable" items="{odm1>/results}">
<columns>
<Column>
<Text text="OrderId" />
</Column>
<!-- ... -->
</columns>
<ColumnListItem>
<Text text="{odm1>OrderID}" />
<!-- ... -->
</ColumnListItem>
</Table>
</Page>
</mvc:View>
这是控制器代码:
sap.ui.define([
"sap/ui/core/mvc/Controller",
"sap/ui/model/odata/v2/ODataModel"
], function (Controller, ODataModel) {
"use strict";
return Controller.extend("c.g.odataapp2.controller.Root", {
onInit: function () {
var oModel = new ODataModel("https://cors-anywhere.herokuapp.com/https://services.odata.org/V2/Northwind/Northwind.svc/Orders?$format=json");
this.getView().setModel(oModel, "odm1");
}
});
});
我需要设置其他任何内容吗(文档中未提及)?
我已经使用了代理/https/...link.... 但似乎无效。
英文:
I am trying to use Northwind data service (OData V2) in my SAPUI5 app. However, I am not able to get any data at all from the server.
This is my XML view:
<mvc:View controllerName="c.g.odataapp2.controller.Root"
xmlns:mvc="sap.ui.core.mvc"
displayBlock="true"
xmlns="sap.m">
<Page id="page" title="{i18n>title}">
<Table id="idOrdersTable" items="{odm1>/results}">
<columns>
<Column>
<Text text="OrderId" />
</Column>
<!-- ... -->
</columns>
<ColumnListItem>
<Text text="{odm1>OrderID}" />
<!-- ... -->
</ColumnListItem>
</Table>
</Page>
</mvc:View>
This is controller code:
sap.ui.define([
"sap/ui/core/mvc/Controller",
"sap/ui/model/odata/v2/ODataModel"
], function (Controller, ODataModel) {
"use strict";
return Controller.extend("c.g.odataapp2.controller.Root", {
onInit: function () {
var oModel = new ODataModel("https://cors-anywhere.herokuapp.com/https://services.odata.org/V2/Northwind/Northwind.svc/Orders?$format=json");
this.getView().setModel(oModel, "odm1");
}
});
});
Do I need to set up anything else (Not mentioned in the docs)?
I have used proxy/https/...link.... but this seems to be not working as either.
答案1
得分: 0
我猜测,代理服务“herokuapp.com”不再工作。
尝试从您的URL中移除这部分。这将导致CORS错误。
要解决此问题,请按照文档中的建议操作。
由于同源策略(跨源资源共享 - CORS)而导致请求失败
英文:
I guess, the proxy-service "herokuapp.com" is not working anymore.
Try to remove the part from your URL. This will result in a CORS-error.
To fix this follow the suggestions from the documentation
Request Fails Due to Same-Origin Policy (Cross-Origin Resource Sharing - CORS)
答案2
得分: 0
以下是翻译好的部分:
-
应用程序代码
- 无效的服务URL:在将字符串传递给
ODataModel
构造函数时,字符串需要指向OData服务提供服务的资源,该资源是服务的$metadata
文档。因此,需要从字符串中删除Orders?$format=json
。 - 在视图定义中,将
items="{odm1>/results}
替换为items="{odm1>/Orders}
。
- 无效的服务URL:在将字符串传递给
-
CORS的问题
公共演示代理服务器
cors-anywhere.herokuapp.com
不再可以直接使用,除非首先请求临时访问权限(请参阅相关公告)。取而代之,从文档主题"由于同源策略(跨域资源共享 - CORS)而导致请求失败"的“解决方案”部分继续。展开对应于您的开发环境的子部分。
如果您在本地开发而没有在SAP BTP中设置目标:
- 展开并按照子部分“本地开发:配置本地代理”的步骤进行操作
- 或者使用
ui5-middleware-simpleproxy
。以下是带有中间件的示例ui5.yaml
配置:https://github.com/boghyon/gitpod-ui5-basic/blob/main/ui5.yaml。请记住在终端中预先执行npm install ui5-middleware-simpleproxy --save-dev
。要在ui5.yaml
中配置ui5-middleware-simpleproxy
以使用来自odata.org
的Northwind服务,您可以设置:mountPath: /myODataService
configuration/baseUri: https://services.odata.org
- 最后,在您的控制器代码中:<pre><code>new ODataModel(<strong>"/myODataService</strong>/V2/Northwind/Northwind.svc"); // without .../Orders?$format=json</code></pre>
* 除了OData V4 TripPin服务,
odata.org
的服务目前不支持CORS。要了解CORS的一般信息,请参阅同源策略和CORS(跨源资源共享)。如果服务不支持CORS,服务可能会报告模糊的报告,例如:
- 客户端发送一个带有方法
OPTIONS
的预检请求,以查看服务器允许哪种类型的请求。 - 服务器回复不理解该
OPTIONS
请求。 - 客户端报告
"OPTIONS ... 501 (Not Implemented)"
。
简而言之
总的来说,来自odata.org
的OData服务维护不良,不完整,并存在许多问题,其中一些问题我已经在https://github.com/OData/ODataSamples/issues?q=is%3Aissue+author%3Aboghyon上报告了。
odata.org
的服务也不支持生成CSRF令牌。在为v2.ODataModel
类定义构造函数设置时,添加tokenHandling: false
:
<!-- language-all: lang-js -->
<pre><code>new ODataModel({ // V2
serviceUrl: "/myMountOrDestinationPath/...",
preliminaryContext: true,
defaultBindingMode: "TwoWay",
<strong>tokenHandling: false,</strong> // prevents "HEAD ... 501 (Not Implemented)" from odata.org services
// ...
})</code></pre>
👉 而不是引用来自odata.org
的服务,可以查看这篇博文,其中提供了由SAP维护的替代样例OData服务:“新的SAP Gateway演示系统可用”。
英文:
<!-- language-all: lang-js -->
There are mainly two issues in the question:
-
Application code
- Invalid service URL: when passing a string to the
ODataModel
constructor, the string needs to be pointing to the resource where the OData service serves the service$metadata
document. Therefore,Orders?$format=json
needs to be removed from the string. - Replace
items="{odm1>/results}
withitems="{odm1>/Orders}
in the view definition.
- Invalid service URL: when passing a string to the
-
Issue with CORS *
The public demo proxy server
cors-anywhere.herokuapp.com
is no longer directly usable without having a temporary access requested first (See the related announcement).Instead, continue with the "Resolution" section from the documentation topic "Request Fails Due to Same-Origin Policy (Cross-Origin Resource Sharing - CORS)". Expand the subsection corresponding to your development environment.
In case you're working locally without setting a destination in SAP BTP:
- Expand and follow the subsection "Local Development: Configure a local proxy"
- Or make use of the
ui5-middleware-simpleproxy
. Here is a sampleui5.yaml
config with the middleware: https://github.com/boghyon/gitpod-ui5-basic/blob/main/ui5.yaml. Keep in mind to executenpm install ui5-middleware-simpleproxy --save-dev
in your terminal beforehand. To configure theui5-middleware-simpleproxy
inui5.yaml
for consuming the Northwind service fromodata.org
, you could set:mountPath: /myODataService
configuration/baseUri: https://services.odata.org
- And finally in your controller code: <pre><code>new ODataModel(<strong>"/myODataService</strong>/V2/Northwind/Northwind.svc"); // without .../Orders?$format=json</code></pre>
* With the exception of the OData V4 TripPin service, services from
odata.org
currently don't support CORS. To learn about CORS in general, see Same origin Policy and CORS (Cross-origin resource sharing).If the service doesn't support CORS, the service might report a vague report, for example:
- Client sends a preflight request with the method
OPTIONS
to see what kind of requests are allowed by the server. - Server responds that it doesn't understand that
OPTIONS
request. - Client reports
"OPTIONS ... 501 (Not Implemented)"
.
TL;DR
In general, OData services from odata.org
are poorly maintained, incomplete, and has many issues some of which I already reported at https://github.com/OData/ODataSamples/issues?q=is%3Aissue+author%3Aboghyon.
Services from odata.org
don't support generating CSRF tokens either. Add tokenHandling: false
when defining the constructor settings for the v2.ODataModel
class:
<!-- language-all: lang-js -->
<pre><code>new ODataModel({ // V2
serviceUrl: "/myMountOrDestinationPath/...",
preliminaryContext: true,
defaultBindingMode: "TwoWay",
<strong>tokenHandling: false,</strong> // prevents "HEAD ... 501 (Not Implemented)" from odata.org services
// ...
})</code></pre>
👉 Instead of referring to services from odata.org
, take a look at this blog post for alternative sample OData services maintained by SAP: "New SAP Gateway Demo System available".
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论