doufu9947 2018-02-02 15:21
浏览 264

Ajax在ServiceWorkers中发布请求

Is it possible to make ajax post request in ServiceWorkers execution?

I have a Service Worker registered that just "listen" for a push notification.

I need to call a PHP function (in order to read some data from my database) during the execution of the Service Worker (when receiving the push notification), but I'm not able to do it. When I call the ajax post it goes to "error" section and the error is "No Transport" (I tried to add the "jQuery.support.cors = true;" like suggested in other thread, but this not fixed the issue).

Here below the serviceworker code.

Is it impossible to do what I'm trying to do, or I'm doing something wrong?

    var document = self.document = {parentNode: null, nodeType: 9, toString: function() {return "FakeDocument"}};
var window = self.window = self;
var fakeElement = Object.create(document);
fakeElement.nodeType = 1;
fakeElement.toString=function() {return "FakeElement"};
fakeElement.parentNode = fakeElement.firstChild = fakeElement.lastChild = fakeElement;
fakeElement.ownerDocument = document;

document.head = document.body = fakeElement;
document.ownerDocument = document.documentElement = document;
document.getElementById = document.createElement = function() {return fakeElement;};
document.createDocumentFragment = function() {return this;};
document.getElementsByTagName = document.getElementsByClassName = function() {return [fakeElement];};
document.getAttribute = document.setAttribute = document.removeChild = 
document.addEventListener = document.removeEventListener = 
function() {return null;};
document.cloneNode = document.appendChild = function() {return this;};
document.appendChild = function(child) {return child;};

importScripts('js/jquery.js');  



self.addEventListener('push', function(event) {

    jQuery.support.cors = true;

    var endpoint = "";

    if (event.data) {
        endpoint = event.data.text();
    }

    var data = {
        query: "SELECT * FROM [TABLE] WHERE ENDPOINT  = '" + endpoint + "'"
    };

    $.ajax({
        data: data,
        method: "POST",
        url: 'ExecuteQueryJquery.php',
        dataType: 'json',
        success: function (obj, textstatus) {
            var o = obj;
        },
        error: function (obj, textstatus) {
            var o = obj;
        }
    });

});
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 BP神经网络控制倒立摆
    • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
    • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
    • ¥30 Unity接入微信SDK 无法开启摄像头
    • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
    • ¥20 cad图纸,chx-3六轴码垛机器人
    • ¥15 移动摄像头专网需要解vlan
    • ¥20 access多表提取相同字段数据并合并
    • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
    • ¥20 Java-Oj-桌布的计算