“This is my 33rd day of participating in the First Challenge 2022. For details: First Challenge 2022”

Error message 1

Access to XMLHttpRequest at ‘http://localhost:8081/https://services.odata.org/V2/Northwind/Northwind.svc/$metadata?sap-language=EN’ from origin ‘http://localhost:8080’ has been blocked by CORS policy: Request header field maxdataserviceversion is not allowed by Access-Control-Allow-Headers in preflight response.

The reason is thatmaxdataserviceversionThis field, which is related to the OData service version, does not appear in the server configurationAccess-Control-Allow-HeadersArray, thus causing cross-domainCORS error.

maxdataserviceversionThis field can be found in the Chrome Developer Tools Network TAB:

Solution:

app.all(The '*'.function(req, res, next) {
    res.header("Access-Control-Allow-Origin"."*");
    res.header("Access-Control-Allow-Headers"."*");
    res.header("Access-Control-Allow-Methods"."PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By".'9.0.0');
    next();
  });
Copy the code

willAccess-Control-Allow-HeadersChange the value of * to *. Error disappear:

Error 2

The metadata corresponding to the following URL cannot be displayed properly in the browser. The desired result is that after entering the URL in the address bar, we should see the actual content of the metadata after http://localhost:8081/ in XML format:

http://localhost:8081/https://services.odata.org/V2/Northwind/Northwind.svc/$metadata?sap-language=EN

Normally, the request header field is: Accept: application/ XML

If the send method is passed as a string, the content-type of the response structure is automatically set to text/ HTML:

This can be observed in Postman:

We can use it in our proxy server implementationres.setMethod can be used toContent-TypeManually change toapplication/xml:

The metadata obtained through the proxy server in XML format should be displayed in the browser:

Error message 3

The /$batch resource only supports POST method request

The header field contains the boundary value:

For the $batch operation, the Accept value is multipart/mixed:

They did hang up:

OData batch requests are typically used when consumers want to perform multiple independent HTTP calls and want to avoid multiple server roundtrips.

Example code for sending batch requests using OData Model in SAP UI5 is as follows:

var tmpModel = new ODataModel("https://xxyyzz.com/sap/opu/odata/<your_service>_SRV/".true);
tmpModel.setDefaultBindingMode(sap.ui.model.BindingMode.TwoWay);
tmpModel.setUseBatch(true);
this.getView().setModel(tmpModel, "tmpModel");
tmpModel.setDeferredGroups(["foo"]);
var mParameters = {groupId:"foo".success:function(odata, resp){ console.log(resp); },error: function(odata, resp) { console.log(resp); }};

for(var m=0; m<oPayload.length; m++) {
	tmpModel.update("/YOUR_ENTITYSet(Key1='Valu1',Key2='Value2')", oPayload[m], mParameters);
}
tmpModel.submitChanges(mParameters);
Copy the code