JavaScript 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.
The 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.
The above test did verify the conclusion. However when I change the approach to send request into ES6 fetch API,
<%@page language="abap" %><%@extension name="htmlb" prefix="htmlb" %><!DOCTYPE html><html><head><title>Jerry Test Stateful</title></head><body><button onclick="fire()">Fire two request</button><script>function wrapperOnFetch(url){
fetch(url).then(function(response){
return response.json();
}).then(function(json){
console.log(url + ":" + json.message);
});}function fire(){
wrapperOnFetch("first.json");
wrapperOnFetch("second.json");}</script></body></html>
the testing request for stateful application looks as below this time:
The 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:
why the latest ES6 API causes such discrepancy with known conclusion? Just compare the cookie of requests sent via these two kinds of API:
Through 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”.
function wrapperOnFetch(url){
// enable session cookie sent with request fetch(url,{ credentials:"include" }).then(function(response){
return response.json();
}).then(function(json){
console.log(url + ":" + json.message);
});}
After this change the stateful BSP application behaves as expected: the requests are handled in sequence.
要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2703941/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 「震驚」你可能需要一個假的 Fetch APIAPI
- 10個需要注意的SQL問題SQL
- 安裝rac時需要注意的問題
- 除錯使用了函式模組的程式時需要注意的一個小問題除錯函式
- 運用mysqldump 工具時需要注意的問題MySql
- javascript變數宣告需要注意的一個地方JavaScript變數
- 在下載opencv等類似的包時,需要注意到的一個大問題!OpenCV
- IBM面試需要注意的幾個問題IBM面試
- 手機遊戲設計需要注意的7個問題遊戲設計
- 教育培訓APP開發時需要注意的問題APP
- 選擇代理IP時需要注意的三大問題
- Javascript需要注意的幾個運算子JavaScript
- 挑選http時候需要注意什麼問題HTTP
- 做聚合支付代理需要注意的這幾個問題?
- 11gRAC安裝需要注意的幾個問題
- 資料補丁中需要注意的幾個問題
- visual studio建立專案時需要注意的問題
- 燒錄Mac OS系統光碟時需要注意的問題Mac
- js Date()建構函式引數需要注意一個問題JS函式
- Python import 時要注意的幾個問題PythonImport
- 配置Oracle RAC需要注意的問題Oracle
- JavaScript Fetch API請求和響應JavaScriptAPI
- Oracle remap_schema需要注意的問題OracleREM
- Python初學者需要注意的問題Python
- 檔案上傳需要注意的問題
- insert append需要注意的問題APP
- SQL SERVER建立索引需要注意的問題SQLServer索引
- python合併多個csv檔案需要注意的問題(合併多個列名問題)Python
- Python讀書筆記:需要注意的70個小問題Python筆記
- 網路爬蟲設計中需要注意的幾個問題爬蟲
- 使用blueZ進行藍芽程式設計時需要注意的問題藍芽程式設計
- Promise使用時應注意的問題Promise
- 多個DW同時更新,且表中有關係存在,需要注意的擊點問題。 (轉)
- 關於 Angular Universal 應用執行時需要 Browser API 的問題AngularAPI
- golang split需要注意的一個點Golang
- 用Unity做個遊戲(一) - 建立專案時需要注意的點Unity遊戲
- 【JavaScript】第一個Demo和一個問題JavaScript
- 三,ES6中需要注意的特性(重要)