Reply to batch request:
Parse with the appropriate handler:
The handler accept field: multipart/mixed
First, according to the response field, get the content-type:
content-type field, separated by ;
:
media type:multipart/mixed
Returns a JSON object with mediaType and boundary properties:
The handler for batch requests is batchParser
:
Execute the next line of the readBatch method:
An example of boundary: batchresponse_16aba97e-4311-41a4-8c64-302727d1d02e
The specific processing logic of readBatch is based on string matching, that is, using indexOf to find the boundary string:
The implementation of readline is to read a single line:
The header field of the first part of the batch is read:
The media type must match the hardcoded value: application/http
The data of the first request count included in the batch has been successfully read:
It has been read successfully, but also dispatch:
The media-type of count is text/plain:
return false, the body of the response has a value, and the data is undefined:
There are 4 available handlers:
text/plain is responsible for reading response.body.
The logic of textParser is the simplest, it directly returns the body field to response.data:
Read the second part:
The content-type of this part is json, so use the json handler:
if (handlerAccepts(handler, cType)) {
var readContext = createReadWriteContext(cType, version, context, handler);
readContext.response = response;
response.data = parseCallback(handler, body, readContext);
return response.data !== undefined;
}
The semantics of the above code is that only after a handler has passed the filtering of function handlerAccepts
, can parseCallback be called, and the parsed result will be passed to response.data
.
The json handler uses the JSON.parse native API to serialize json data.
Serialize successful json data:
The final returned response is an array:
Call the outermost success handler:
The callback function of batch request, inside function _submitBatchRequest
:
More Jerry's original articles, all in: "Wang Zixi":
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。