[JS] Unable to get Content-Disposition in response headers

Article directory

Problem recurrence

insert image description here

  • From the response header, there is Content-Disposition, and then the actual print:
fetch(
  url,
  {
    
    
    method: 'GET',
    headers: {
    
    
      'content-type': 'application/json',
      'X-Requested-With': 'XMLHttpRequest',
    },
    mode: 'cors',
    credentials: 'include',
  },
)
.then((res) => {
    
    
      // 从响应头中获取 Content-Disposition
      console.log('Content-Disposition :', res.headers.get("Content-Disposition")); 	// 打印结果为 null
  })

Solution

  • The above question mentioned cannot be content-dispositionprinted out, even if the server adds this field in the protocol reply packet, but because it is not exposed to the outside, the client cannot get it.

  • The response header Access-Control-Expose-Headersis the switch to control "exposing", which lists which headers can be exposed to the outside as part of the response.

  • So if you want the client to have access to other header information, the server must not only add the header to the header, but also list them Access-Control-Expose-Headersin

  • The backend needs to be added:

response.setHeader("Access-Control-Expose-Headers","Content-Disposition");

Guess you like

Origin blog.csdn.net/qq_45677671/article/details/130748011