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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 10個需要注意的SQL問題SQL
- 安裝rac時需要注意的問題
- 運用mysqldump 工具時需要注意的問題MySql
- 在下載opencv等類似的包時,需要注意到的一個大問題!OpenCV
- visual studio建立專案時需要注意的問題
- C# + html + fetch + API + javascriptC#HTMLAPIJavaScript
- Javascript需要注意的幾個運算子JavaScript
- Python import 時要注意的幾個問題PythonImport
- 選擇代理IP時需要注意的三大問題
- 教育培訓APP開發時需要注意的問題APP
- 使用 sendBeacon 需要注意的問題
- 刷題時需要的注意事項
- JavaScript Fetch API請求和響應JavaScriptAPI
- 做聚合支付代理需要注意的這幾個問題?
- 挑選http時候需要注意什麼問題HTTP
- Python讀書筆記:需要注意的70個小問題Python筆記
- python合併多個csv檔案需要注意的問題(合併多個列名問題)Python
- Oracle remap_schema需要注意的問題OracleREM
- Python初學者需要注意的問題Python
- 網路爬蟲設計中需要注意的幾個問題爬蟲
- Promise使用時應注意的問題Promise
- 關於 Angular Universal 應用執行時需要 Browser API 的問題AngularAPI
- 處理多個會話時的 Cookie 和 Headers 複用問題會話CookieHeader
- 處理多個會話時的 Cookie 和 Headers複用問題會話CookieHeader
- 教育直播系統搭建需要注意的問題
- vue中需要注意的問題總結(上)Vue
- app開發需要注意哪些問題APP
- 資料治理需要注意哪些問題
- 寶鯤財經:市場投資需要注意的幾個問題
- golang split需要注意的一個點Golang
- 企業在實施採購管理時需要注意哪些問題?
- API與API管理,需要注意什麼?API
- 三,ES6中需要注意的特性(重要)
- 面試官:集合使用時應該注意哪些問題?我:應該注意該注意的問題!面試
- Cookie出現兩個同名Key的問題Cookie
- map判斷值是否存在需要注意的問題
- 使用 foreach 使用引用變數需要注意的問題變數
- 電磁流量計在使用需要注意的問題