XMLHttpRequest within service worker

柔情痞子 提交于 2019-12-05 14:06:26

Instead of XMLHttpRequest, you can use the new Fetch API. XMLHttpRequest has been deprecated and is not available in the service worker scope.

There's an example of exactly what you want to achieve in this ServiceWorker Cookbook recipe.

You can use fetch() instead of XHR,as fetch() is a new API which allows you to make requests similar to XHR's, but has a simpler / friendly API.Read more about fetch here. Try this:-

self.addEventListener('push', function(event) { 
    console.log('Push message', event);
  event.waitUntil(
fetch('/myfileWithJson.php').then(function(response){
return response.json();
}).then(function(data){console.log(data);
    return self.registration.showNotification(data.title, {
      body: data.body,
      icon: 'images/icon.png',   
      tag: data.url

    });
}));
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!