[英]JavaScript: Set Content-Type header on Response Object
How can I set the Content-Type
header on a Response
object?如何在
Response
对象上设置Content-Type
标头?
My code so far:到目前为止我的代码:
new Response(
file[1],
{
status: 200,
// i have also tried "contentType", and "ContentType"
"Content-Type": "text/html",
},
)
For background, I am trying to create a custom HTML file and put it in cache with the caches API, and it is defaulting to text/plain.对于背景,我正在尝试创建一个自定义 HTML 文件并使用缓存 API 将其放入缓存中,并且默认为 text/plain。
I figured it out right after posting:发帖后我就明白了:
new Response(
"response body here...",
{
status: 200,
headers: new Headers({
"content-type": "text/html;charset=UTF-8",
}),
},
)
You need to use the Headers constructor and set it under the "headers" key, then you can set whatever response headers you want.您需要使用 Headers 构造函数并将其设置在“headers”键下,然后您可以设置所需的任何响应标头。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.