广告
返回顶部
首页 > 资讯 > 前端开发 > node.js >JavaScript ES6 Fetch API时需要注意的一个Cookie问题是怎样的
  • 800
分享到

JavaScript ES6 Fetch API时需要注意的一个Cookie问题是怎样的

2024-04-02 19:04:59 800人浏览 安东尼
摘要

javascript es6 Fetch api时需要注意的一个Cookie问题是怎样的,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。When

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.

JavaScript ES6 Fetch API时需要注意的一个Cookie问题是怎样的

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.

JavaScript ES6 Fetch API时需要注意的一个Cookie问题是怎样的

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:

JavaScript ES6 Fetch API时需要注意的一个Cookie问题是怎样的

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:

JavaScript ES6 Fetch API时需要注意的一个Cookie问题是怎样的

why the latest ES6 API causes such discrepancy with known conclusion? Just compare the cookie of requests sent via these two kinds of API:

JavaScript ES6 Fetch API时需要注意的一个Cookie问题是怎样的JavaScript ES6 Fetch API时需要注意的一个Cookie问题是怎样的

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”.

JavaScript ES6 Fetch API时需要注意的一个Cookie问题是怎样的

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.

看完上述内容,你们掌握JavaScript ES6 Fetch API时需要注意的一个Cookie问题是怎样的的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注编程网node.js频道,感谢各位的阅读!

--结束END--

本文标题: JavaScript ES6 Fetch API时需要注意的一个Cookie问题是怎样的

本文链接: https://www.lsjlt.com/news/82440.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • JavaScript ES6 Fetch API时需要注意的一个Cookie问题是怎样的
    JavaScript ES6 Fetch API时需要注意的一个Cookie问题是怎样的,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。When...
    99+
    2022-10-19
  • JavaScript中10个需要掌握基础的问题是怎样的
    JavaScript中10个需要掌握基础的问题是怎样的,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。JavaScript 是一...
    99+
    2022-10-19
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作