是否可以在ServiceWorkers执行中发出ajax post请求?
我已经注册了一个服务工作者,只是“收听”推送通知。
我需要在Service Worker的执行期间(当接收推送通知时)调用PHP函数(以便从我的数据库中读取一些数据),但是我无法做到。当我调用ajax帖子时,它转到“错误”部分并且错误是“无传输”(我试图添加“jQuery.support.cors = true;”,就像在其他线程中建议的那样,但这不能解决问题)
下面是serviceworker代码。
我无法做我想做的事情,或者我做错了什么?
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;
}
});
});