fetch发送Form Data请求并携带cookie

今天我们来说说如何fetch发送Form Data请求并携带cookie,直接进入正题好吧,别问我今天为啥不在开始吹两句了,累到一句牛逼不想吹。。。。。。

步骤1:

  设置头部,"Content-Type": "application/x-www-form-urlencoded"

步骤2:

  发送请求的时候采用key=val&key1=val1这种形式进行连接

步骤三:

  直接在本地种好要发送的cookie即可

示例如下

     document.cookie='a=bbbbb'
 
    fetch("url", { method: 'POST', credentials : 'include', headers: { "Content-Type": "application/x-www-form-urlencoded", }, body:'KeyWord=&CatCode=0111_ganjuchengyou&PageIndex=1&PageSize=20&Sort='

}) .then(r => { return r.json() }) .then(r => {         console.log(r) })

请求结果:

  

特别注意:fetch默认是不携带cookie的,要想携带cookie,一定要设置credentials : 'include',

猜你喜欢

转载自www.cnblogs.com/suihang/p/10018405.html