我正在尝试在 chrome 上创建一个推送通知系统。
我有一个从 mysql 获取数据并回显 JSON 的 php,现在我想调用一个函数 getJsonCode(),当推送通知到达时它会被激活并读取 JSON 数据。
在我的 Service Worker 中,我创建了标准函数。问题是,当我使用 XMLHttpRequest 创建 getJsonCode 时,它告诉我它没有定义
self.addEventListener('install', function(event) {
self.skipWaiting();
console.log('Installed', event);
});
self.addEventListener('activate', function(event) {
console.log('Activated', event);
});
self.addEventListener('push', function(event) {
console.log('Push message', event);
getJsonCode();
);
})
getJsonCode() {
codeRequest= new XMLHttpRequest();
codeRequest.open('get', 'myfileWithJson.php', true);
codeRequest.send();
dataJSON = codeRequest.responseText;
console.log('rispostaJSON');
}
是否可以在 Service Worker 中调用这样的外部文件,或者是否存在某种限制?因为我不明白为什么这不起作用
代替XMLHttpRequest
,您可以使用新的获取API. XMLHttpRequest
已被弃用,并且在 Service Worker 范围内不可用。
有一个例子说明了您想要实现的目标这个 ServiceWorker Cookbook 食谱.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)