使用ES6的fetch API读取数据时要注意的一个和cookie相关的坑

简介: 使用ES6的fetch API读取数据时要注意的一个和cookie相关的坑

When I am doing a test of comparison between Stateful and Stateless BSP application ( mentioned in blog Stateless and Stateful – Different behavior in application side ), I meet with a strange issue.


The conclusion is stateful BSP application will handle request sequentially. Suppose in client I send two request A and B to server. Request A takes 3 seconds to finish and B 2 seconds.image.pngThe request is sent via jQuery API.


It means for stateful application, I will observe the following timeline in Chrome network tab:


(1) the start time of both request are almost the same, since I send out two request in client code almost at the same time.


(2) even though the second request itself takes 2 seconds to finish, the total processing time for it is 3 seconds waiting for A to finish first + 2 seconds = 5 seconds in the end.image.pngThe above test did verify the conclusion.

However when I change the approach to send request into ES6 fetch API,image.pngthe testing request for stateful application looks as below this time:image.pngThe two requests are handled simultaneously ( request B only takes 2 seconds to finish, no 3 seconds’ wait time for A to finish this time ), the response of second request returns first before the first, which could be observed in console:image.pngwhy the latest ES6 API causes such discrepancy with known conclusion?

Just compare the cookie of requests sent via these two kinds of API:

image.pngimage.pnghrough comparison I get to know that the session cookie

sap-contextid is not sent together with request triggered by ES6 Fetch API.

Then in Fetch documentation I find out that I need to add option credentials: “include”.

image.pngimage.png



相关文章
|
7月前
|
存储 API
14JavaWeb基础 - Cookie 与 Session API总结
14JavaWeb基础 - Cookie 与 Session API总结
24 0
|
8月前
|
JavaScript 前端开发 API
什么是 Web API 中的 background fetch API
什么是 Web API 中的 background fetch API
54 0
|
8月前
|
Web App开发 JavaScript 前端开发
在 SAP UI5 应用中使用浏览器原生的 Fetch API 发起网络请求试读版
在 SAP UI5 应用中使用浏览器原生的 Fetch API 发起网络请求试读版
36 0
|
JavaScript 前端开发 API
使用ort.js的create方法加载onnx模型报错:Fetch API cannot load file…… URL scheme “file“ is not supported.
使用ort.js的create方法加载onnx模型报错:Fetch API cannot load file…… URL scheme “file“ is not supported.
454 0
使用ort.js的create方法加载onnx模型报错:Fetch API cannot load file…… URL scheme “file“ is not supported.
|
9月前
|
JSON 前端开发 JavaScript
浅谈Fetch API
浅谈Fetch API
107 1
|
前端开发 API
前端知识案例-fetch api进行数据绑定
前端知识案例-fetch api进行数据绑定
68 0
前端知识案例-fetch api进行数据绑定
|
存储 前端开发 API
Servlet API Cookie和Session
我们在前文【和面试官畅谈「HTTP协议」 3.3.6】中已经提及了Servlet在会话管理操作中的Cookie与Session,但在上篇文章中有些过于理论化,在这篇文章中,我们结合具体的API和综合案例来更深刻的理解Cookie和Session。
102 0
Servlet API Cookie和Session
|
JavaScript 前端开发 API
Node.js 支持 fetch API
Node.js 支持 fetch API
279 0
Node.js 支持 fetch API
|
JavaScript 前端开发 API
Node.js 支持 fetch API
Node.js 支持 fetch API
179 0
Node.js 支持 fetch API
|
JSON 前端开发 JavaScript
简单入门Fetch API
简单入门Fetch API
233 0