Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在服务工作者';中使用字符串创建HTML响应;s获取事件_Javascript_Html_Service Worker - Fatal编程技术网

Javascript 在服务工作者';中使用字符串创建HTML响应;s获取事件

Javascript 在服务工作者';中使用字符串创建HTML响应;s获取事件,javascript,html,service-worker,Javascript,Html,Service Worker,我现在正和服务人员混在一起。我正在尝试让“fetch”事件返回自定义HTML,而不是像下面这样的纯文本: self.addEventListener('fetch', function(event) { console.log("Caught a fetch!"); event.respondWith(new Response("I want to return HTML here")); }); 使用respondWith函数是否可以实现这一点 其他任何人都希望这样做-以下是答案:

我现在正和服务人员混在一起。我正在尝试让“fetch”事件返回自定义HTML,而不是像下面这样的纯文本:

self.addEventListener('fetch', function(event) {
  console.log("Caught a fetch!");
  event.respondWith(new Response("I want to return HTML here"));
});

使用respondWith函数是否可以实现这一点

其他任何人都希望这样做-以下是答案:

self.addEventListener('fetch', function(event) {
  console.log("Caught a fetch!");
  event.respondWith(
      new Response("<h1>Hello!</h1>", {
        headers: {'Content-Type': 'text/html'}
      })
   )
});
self.addEventListener('fetch',函数(事件){
log(“抓到一次抓取!”);
事件响应(
新的回应(“你好!”{
标题:{'Content-Type':'text/html'}
})
)
});