简体   繁体   English

如果使用jQuery.ajax或XMLHttpRequest发出请求,则Service Worker获取处理程序不会触发

[英]Service Worker fetch handler not triggering if request is made using jQuery.ajax or XMLHttpRequest

I am using service workers in one of my apps and want to catch some assets requests in "service worker fetch handler" that are made from jQuery ajax but fetch handler is not triggering for these type of requests (including XMLHttpRequest() ), however, I am able to catch other requests(for js, image etc.) in service worker fetch handler. 我在我的一个应用程序中使用服务工作者,并希望在“服务工作程序获取处理程序”中捕获一些资产请求,这些请求是由jQuery ajax创建的,但是获取处理程序不会触发这些类型的请求(包括XMLHttpRequest() ),但是,我能够在服务工作者获取处理程序中捕获其他请求(对于js,图像等)。

The sample code in app.js app.js中的示例代码

$.ajax({
    async: false,
    url: "./Initial.xml",
    dataType: "xml",
    success: function(response) {
       self.initial = response;
    }
});

The sample code in service worker 服务工作者中的示例代码

self.addEventListener('fetch', event => {
    console.log(event.request.url);
    event.respondWith(fetch(myRequest.clone()));
});

Thanks 谢谢

I also faced a similar kind of issue recently. 我最近也遇到过类似的问题。

You should be checking this code in chrome browser. 您应该在Chrome浏览器中检查此代码。 Because, as of now (Jun 2019) firefox is able to catch the given request in fetch handler. 因为,截至目前(2019年6月),firefox能够在获取处理程序中捕获给定的请求。

Coming to chrome browser, its a open bug in chrome. 来到chrome浏览器,它是一个开放的Chrome 错误 By looking at its recent activity, they are working on the fix. 通过查看最近的活动,他们正在努力解决这个问题。

In mean time, if you want it to be working in chrome, just make it as asynchronous call by passing async flag as true . 同时,如果您希望它在chrome中工作,只需将async标志作为true传递给异步调用即可。

$.ajax({
    async: true,
    url: "./Initial.xml",
    dataType: "xml",
    success: function(response) {
       self.initial = response;
    }
});

After this, given fetch handler will be triggered in chrome as well :) 在此之后,给定的提取处理程序也将在chrome中触发:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM