Fetch request how to join session?

the project uses a request made by fetch, and the background says that session can be used to handle session permissions! But how to add session in the request header.

after looking up the information, it is said that request.getSession can get it, but there is some doubt that request, can not be found in the request encapsulated by fetch. How to encapsulate it.

export const get = (url, query = {}, options = {}) => {
  const defaultOpt = {
    method: "GET",
    timeout: requestTimeOut,
    credentials: "include",
    headers: { ...options }
  }

  defaultOpt.headers = completeHeader(defaultOpt.headers)
  console.log(defaultOpt)
  return fetch(getUrl(url, query), defaultOpt).then(checkStatus).then(parseJSON)
}

above is one of the examples

the Internet also says to join credentials: "include", but I still don"t quite understand.

Mar.30,2021

generally the sessionid is placed in the cookie, and the session is saved in the background. When sending a request to the server, the cookie, in the same domain does not need to be manually set


that is, credentials is added. The fetch request header cannot get sessionid, plus by default without cookie, backend.


fetch does not have cookie by default. If it is in the same domain, add mode

.
const defaultOpt = {
    mode: "same-origin",
    credentials: "same-origin",
    ...
}
Menu