SAP UI5 利用在发送 OData batch 申请之前,会通过下列的_createBatchRequest办法结构 batch 申请对象:

var oBatchRequest = that._createBatchRequest(aReadRequests);

该申请对象的 data 字段里,蕴含 batch 具体的 payload:

  • Invoices?$skip=0&$top=100&$orderby=ShipperName%20asc
  • Invoices/$count

该申请头部字段 Accept 为 multipart/mixed:

而后通过 oWrappedBatchRequestHandle.oRequestHandle = that._submitBatchRequest(oBatchRequest, aBatchGroup, fnSuccess, fnError) 办法进行提交。

token handling 标记位为 true,且办法不为 POST,因而在执行 batch 操作之前,先要获取 CSRF token:

进入函数refreshSecurityToken.

结构发动 token 申请的 request 对象:

url 为:https://services.odata.org/V2...

首先尝试 head 申请,如果报错,再切换成 get 申请:

// Initially try method "HEAD", error handler falls back to "GET" unless the flag forbids HEAD request        if (this.bDisableHeadRequestForToken) {            mTokenRequest.request = requestToken("GET", handleGetError);        } else {            mTokenRequest.request = requestToken("HEAD", handleHeadError);        }

申请 token 的 HTTP 申请的 Content-type 设置逻辑,和标记位 bJson 无关:

request 对象:

最重要的头部字段 x-csrf-token, 值被填充成 fetch:

function requestToken(sRequestType, fnError) {            // trigger a read to the service url to fetch the token            oRequest = that._createRequest(sUrl, "", sRequestType,                that._getHeaders(undefined, true), null, null, !!bAsync);            oRequest.headers["x-csrf-token"] = "Fetch";            return that._request(oRequest, handleSuccess, fnError, undefined, undefined, that.getServiceMetadata());        }

执行了 head 申请后,响应的状态码是 200,然而 responseText 字段值是空的。

还是进入 success callback:

应用 handler 读取 token 申请的 response,这个 handler 反对的 content-type 类型:application/atomsvc+xml;q=0.8, application/json;odata=fullmetadata;q=0.7, application/json;q=0.5, /;q=0.1

这里因为 response.body 是空的,因而进入不了 dispatchHandler的解决逻辑:

而后进入 refreshToken 的 callback:

当然是拿不到 token 的:

进入 else 分支:

革除所有相干的 token 标记位:

ODataModel.prototype.resetSecurityToken = function() {        delete this.oSharedServiceData.securityToken;        delete this.oHeaders["x-csrf-token"];        delete this.pSecurityToken;    };

resolve 一个空的 token 给 callback:

这个 head 申请的响应码为 200,然而响应头部没有附带 csrf token:

更多Jerry的原创文章,尽在:"汪子熙":