服务工作者和 AJAX [英] Service Worker and AJAX

查看:52
本文介绍了服务工作者和 AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 AJAX 来检索我想在用户端显示的推送通知的详细信息,但它还不起作用.

I am trying to use AJAX to retrieve the details for the push notification I want to display on the users end, but it doesn't work yet.

/*
*
*  Push Notifications codelab
*  Copyright 2015 Google Inc. All rights reserved.
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*      https://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or     implied.
*  See the License for the specific language governing permissions and
*  limitations under the License
*
*/

 // Version 0.1

//'use strict';

console.log('Started', self);

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);

    var title = 'Push message';
    var xhttp = new XMLHttpRequest();

    xhttp.open("GET", "https://www.domain.nl/devtest/1.php", false);
    xhttp.send();
    title = xhttp.responseText;

    event.waitUntil(
        self.registration.showNotification(data, {
            'body': 'The Message',
            'icon': 'images/icon.png'
        })
    );
});

当我使用 GCM 向客户端发送推送通知时,Chrome 在 Service Worker 上出现此错误:

When I use GCM to send a push notification to the client, Chrome gives this error on the service worker:

sw.js:39 Uncaught ReferenceError: XMLHttpRequest is not defined

推荐答案

XMLHttpRequest 已被弃用,它在 Service Worker 范围内不可用.您可以使用 代替 XMLHttpRequest获取 API.

XMLHttpRequest has been deprecated and it's not available in the Service Worker scope. Instead of XMLHttpRequest, you can use the Fetch API.

这篇关于服务工作者和 AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆