fetch 自动加cookie_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,

Jerry Test StatefulFire two request

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值