溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

JavaScript ES6 Fetch API時(shí)需要注意的一個(gè)Cookie問題是怎樣的

發(fā)布時(shí)間:2021-09-30 15:45:42 來源:億速云 閱讀:117 作者:柒染 欄目:web開發(fā)

JavaScript ES6 Fetch API時(shí)需要注意的一個(gè)Cookie問題是怎樣的,相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

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時(shí)需要注意的一個(gè)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時(shí)需要注意的一個(gè)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時(shí)需要注意的一個(gè)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時(shí)需要注意的一個(gè)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時(shí)需要注意的一個(gè)Cookie問題是怎樣的JavaScript ES6 Fetch API時(shí)需要注意的一個(gè)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時(shí)需要注意的一個(gè)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.

看完上述內(nèi)容,你們掌握J(rèn)avaScript ES6 Fetch API時(shí)需要注意的一個(gè)Cookie問題是怎樣的的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI