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.
There are mainly two issues in the question:
Application code
ODataModelconstructor, the string needs to be pointing to the resource where the OData service serves the service$metadatadocument. Therefore,Orders?$format=jsonneeds to be removed from the string.items="{odm1>/results}withitems="{odm1>/Orders}in the view definition.Issue with CORS *
The public demo proxy server
cors-anywhere.herokuapp.comis 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:
ui5-middleware-simpleproxy. Here is a sampleui5.yamlconfig with the middleware: https://github.com/boghyon/gitpod-ui5-basic/blob/main/ui5.yaml. Keep in mind to executenpm install ui5-middleware-simpleproxy --save-devin your terminal beforehand. To configure theui5-middleware-simpleproxyinui5.yamlfor consuming the Northwind service fromodata.org, you could set:mountPath: /myODataServiceconfiguration/baseUri: https://services.odata.org* With the exception of the OData V4 TripPin service, services from
odata.orgcurrently 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:
OPTIONSto see what kind of requests are allowed by the server.OPTIONSrequest."OPTIONS ... 501 (Not Implemented)".TL;DR
In general, OData services from
odata.orgare 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.orgdon't support generating CSRF tokens either. AddtokenHandling: falsewhen defining the constructor settings for thev2.ODataModelclass: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".