fetch发送Form Data请求并携带cookie

2023-05-12,,

今天我们来说说如何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',

fetch发送Form Data请求并携带cookie的相关教程结束。

《fetch发送Form Data请求并携带cookie.doc》

下载本文的Word格式文档,以方便收藏与打印。