Initial loading of metadata fails due to “501 (Not

2020-02-11 07:43发布

问题:

I am trying to consume Northwind R/W OData service link:

http://services.odata.org/V3/(S(frik5l2zde0sxh4jiifyhqo4))/OData/OData.svc/ as

proxy/http/services.odata.org/V3/(S(frik5l2zde0sxh4jiifyhqo4))/OData/OData.svc

which is working fine on local testing. Now when I want to ftp it to my domain, it's not working...

NetworkError: 404 Not Found - http://{mydomain}/proxy/http/services.odata.org/V3/(S(frik5l2zde0sxh4jiifyhqo4))/OData/OData.svc/$metadata

Using it without proxy like http://services.odata.org/V3/(S(frik5l2zde0sxh4jiifyhqo4))/OData/OData.svc/ gives error as

NetworkError: 501 Not Implemented

code:

onInit: function() {
  var serviceUrl = "http://services.odata.org/V3/(S(frik5l2zde0sxh4jiifyhqo4))/OData/OData.svc/";
  var oModel = new sap.ui.model.odata.ODataModel(serviceUrl);
  oModel.oHeaders = {
    "DataServiceVersion": "3.0",
    "MaxDataServiceVersion": "3.0"
  }
  sap.ui.getCore().setModel(oModel, "products");
}

回答1:

As you're using Northwind, I believe it's only for development. So you can make use of https://cors-anywhere.herokuapp.com/ to access cross origin resources.

var oModel = new ODataModel({
  serviceUrl: "https://cors-anywhere.herokuapp.com/https://services.odata.org/V2/(S(frik5l2zde0sxh4jiifyhqo4))/OData/OData.svc/"
});

Or the other way would be to disable security flag in chrome for development.



回答2:

The problem is that services from odata.org currently don't support CORS. To learn about what it is, see:

--> What is CORS / Same Origin Policy?

In short, this is what happens in your case:

  1. Client sends a preflight request, with 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 "OPTIONS ... 501 (Not Implemented)".

In order to circumvent this, you have to go through a proxy server. Please go through the topic First-Aid Kit: Request Fails Due to Same-Origin Policy (Cross-Origin Resource Sharing - CORS) to learn how to use a proxy server according to your development environment.


Example: SAP Web IDE and SAP Cloud Platform (SCP)

  1. Define a corresponding destination on SCP:


    Source: Create Northwind Destination
    The SCP will then act as a proxy server for the Northwind service. This works since the same origin policy doesn't apply to servers in contrast to browsers.

  2. To create a link between SCP and Web IDE, edit neo-app.json and manifest.json from your Web IDE project folder accordingly:

    In neo-app.json, add the following route:

    {
      "routes": [
        ...,
        {
          "path": "/destinations/northwind",
          "target": {
            "type": "destination",
            "name": "northwind"
          },
          "description": "Northwind OData Service"
        }
      ]
    }
    
  3. And in manifest.json, the following data source:

    "sap.app": {
      "dataSources": {
        "invoiceRemote": {
          "uri": "/destinations/northwind/V2/Northwind/Northwind.svc/",
          "type": "OData",
          "settings": {
            "odataVersion": "2.0"
          }
        }
      }
    }
    

Whenever the browser sends an XHR request with /destinations/northwind/ at the beginning of the request URI, Web IDE passes the request to the proxy server (SCP) which will then fetch the data on behalf of the client.


The public proxy service cors-anywhere.herokuapp.com works too, but it preliminarily sends every single request with a preflight request sequentially (two requests every time) since preflight requests aren't cached by that proxy server by default source. Also the number of requests per period is limited there.



标签: odata sapui5