简体   繁体   中英

Fetch js how do i send post request with cookies

fetch("https://learn.algoritmika.org/api/v1/projects/comment/31315833", {
  "headers": {
    "accept": "*/*",
    "accept-language": "ru,en;q=0.9",
    "content-type": "application/json",
    "sec-ch-ua": "\"Chromium\";v=\"104\", \" Not A;Brand\";v=\"99\", \"Yandex\";v=\"22\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\"",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-origin"
  },
  "referrer": "https://learn.algoritmika.org/my-class?category=new&filter=class_projects&projectId=31315833",
  "referrerPolicy": "strict-origin-when-cross-origin",
  "body": "{\"message\":\"data\"}",
  "method": "POST",
  "mode": "cors",
  "credentials": "include"
});

im need to replace "data" to cookies im tried to replace {\"message\":document.cookie} but its doesn't help

Variables aren't expanded inside a string. And even if they were, you forgot to put quotes around the value of document.cookie .

Don't try to create JSON by hand with string operations, always use JSON.stringify() to ensure that everything is quoted and escaped properly.

"body": JSON.stringify({message: document.cookie}),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM