• 使用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的原创文章,请关注公众号"汪子熙":

  • 相关阅读:
    JS版Stopwatch计时器
    【学习笔记】——网络营销2(赢利)
    CMMI 3级精简并行过程综述
    【学习笔记】——网络营销1(基础概念)
    ASP.NET MVC路由匹配检测组件的代码分析
    学习笔记(一)
    【学习笔记】——网络营销3(市场策略)
    jQuery.API源码深入剖析以及应用实现(4) - 选择器篇(下)
    关于实现B/S与C/S平台之间功能通用性的设计思路
    【学习笔记】——网络营销6(搜索引擎优化)
  • 原文地址:https://www.cnblogs.com/sap-jerry/p/13568442.html
Copyright © 2020-2023  润新知