无法在SAPUI5应用程序中使用OData V2(Northwind)数据。

huangapple go评论69阅读模式
英文:

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

以下是翻译好的部分:

  1. 应用程序代码

    • 无效的服务URL:在将字符串传递给ODataModel构造函数时,字符串需要指向OData服务提供服务的资源,该资源是服务的$metadata文档。因此,需要从字符串中删除Orders?$format=json
    • 在视图定义中,将items="{odm1>/results}替换为items="{odm1>/Orders}
  2. 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服务,您可以设置:
      1. mountPath: /myODataService
      2. configuration/baseUri: https://services.odata.org
      3. 最后,在您的控制器代码中:<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,服务可能会报告模糊的报告,例如:

    1. 客户端发送一个带有方法OPTIONS的预检请求,以查看服务器允许哪种类型的请求。
    2. 服务器回复不理解该OPTIONS请求。
    3. 客户端报告&quot;OPTIONS ... 501 (Not Implemented)&quot;

简而言之

总的来说,来自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:

  1. 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=&quot;{odm1&gt;/results} with items=&quot;{odm1&gt;/Orders} in the view definition.
  2. 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 sample ui5.yaml config with the middleware: https://github.com/boghyon/gitpod-ui5-basic/blob/main/ui5.yaml. Keep in mind to execute npm install ui5-middleware-simpleproxy --save-dev in your terminal beforehand. To configure the ui5-middleware-simpleproxy in ui5.yaml for consuming the Northwind service from odata.org, you could set:
      1. mountPath: /myODataService
      2. configuration/baseUri: https://services.odata.org
      3. 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:

    1. Client sends a preflight request with the method OPTIONS to see what kind of requests are allowed by the server.
    2. Server responds that it doesn't understand that OPTIONS request.
    3. Client reports &quot;OPTIONS ... 501 (Not Implemented)&quot;.

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".

huangapple
  • 本文由 发表于 2023年6月21日 22:54:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76524629.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定