When we use the OData model in our SAP UI5 application to read data from the remote server, please refer to my article for the specific implementation details: SAP UI5 Beginner Tutorial Part 24 - How to Use the OData Data Model , which can be found in Chrome In the developer tools, a request for metadata automatically made by the SAP UI5 framework was observed.
Requested url:
https://services.odata.org/V2/Northwind/Northwind.svc/$metadata?sap-language=EN
The request has no content-type field, only one accept field with the value: application/xml
There is also a MaxDataServiceVersion with a value of 3.0
In the header field of the response, there is no Accept field, only the Content-Type field, pay attention to the case:
The specific code location that initiates the HTTP request: the _loadMetadata method of ODataMetadata:
Trigger _loadMetadata in the constructor:
Create a request object based on the url:
Get supported languages for HTTP requests according to the following API: Accept-Language
sap.ui.getCore().getConfiguration().getLanguageTag()
Calculated to be: en-US
The bAsync flag is true, which means this is an asynchronous request:
The withCredentials flag is false:
Then use the traditional promise API to make the request:
If the metadata is loaded successfully, execute the initialization logic in its callback:
if (!this.oMetadata.isLoaded()) {
this.oMetadata.attachFailed(this.onMetadataFailed);
}
Next, call the OData API, pass in the request object just constructed, and send the request:
Enter datajs.js, first prepareRequest:
normalizeHeaders: Normalize the header fields, in fact, convert the header fields to lowercase:
Then call invokeRequest to send:
Hand over to httpClient:
The bottom layer of datajs.js is still based on XHR, namely XmlHttpRequest:
Use the browser's native XMLHttpRequest directly:
The fallback mechanism here uses the very old ActiveXObject, which modern browsers will not implement:
var createXmlHttpRequest = function () {
/// <summary>Creates a XmlHttpRequest object.</summary>
/// <returns type="XmlHttpRequest">XmlHttpRequest object.</returns>
if (window.XMLHttpRequest) {
return new window.XMLHttpRequest();
}
var exception;
if (window.ActiveXObject) {
try {
return new window.ActiveXObject("Msxml2.XMLHTTP.6.0");
} catch (_) {
try {
return new window.ActiveXObject("Msxml2.XMLHTTP.3.0");
} catch (e) {
exception = e;
}
}
} else {
exception = { message: "XMLHttpRequest not supported" };
}
throw exception;
};
Finally call xhr.open:
Use send api for request sending.
More Jerry's original articles, all in: "Wang Zixi":
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。