2016-07-15 13 views
5

Tôi đang cố gắng sử dụng AJAX để truy xuất thông tin chi tiết về thông báo đẩy mà tôi muốn hiển thị trên đầu người dùng, nhưng nó vẫn chưa hoạt động.Service Worker và AJAX

/* 
* 
* 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' 
     }) 
    ); 
}); 

Khi tôi sử dụng GCM để gửi một thông báo đẩy cho khách hàng, Chrome cung cấp cho lỗi này trên người lao động dịch vụ:

sw.js: 39 của router ReferenceError: XMLHttpRequest không được định nghĩa

+0

trùng lặp có thể xảy ra của [XMLHttpRequest trong lao động dịch vụ] (http://stackoverflow.com/questions/37112425/xmlhttprequest-within-service-worker) – Marco

Trả lời

9

XMLHttpRequest không được dùng nữa và không có sẵn trong phạm vi Công nhân dịch vụ. Thay vì XMLHttpRequest, bạn có thể sử dụng Fetch API.

+1

Họ lấy ra nghiêm túc ajax thường xuyên từ người lao động dịch vụ? WTF? Có vẻ như tôi sẽ không sử dụng nó trong một thời gian dài. –

+0

@MichaelHanon, Yea của nó đúng, lấy là cách duy nhất. cũng xem https://stackoverflow.com/a/46727718/632951 – Pacerier

+0

https://www.fxsitecompat.com/en-CA/docs/2015/xmlhttprequest-is-no-longer-available-in-service-workers/ – Pacerier

0

Để làm điều này, chúng ta có thể đặt các tham số phương pháp và cơ thể trong tùy chọn fetch().

fetch(url, { 
    method: 'post', 
    headers: { 
     "Content-type": "application/x-www-form-urlencoded; charset=UTF-8" 
    }, 
    body: 'foo=bar&lorem=ipsum' 
    }) 
    .then(json) 
    .then(function (data) { 
    console.log('Request succeeded with JSON response', data); 
    }) 
    .catch(function (error) { 
    console.log('Request failed', error); 
    }); 

Nếu bạn muốn yêu cầu tìm nạp bằng thông tin xác thực như cookie, bạn nên đặt thông tin đăng nhập của yêu cầu để "bao gồm".

fetch(url, { 
    credentials: 'include' 
}) 
+1

Nhân viên dịch vụ không có quyền truy cập vào cookie. –