简体   繁体   English

如何在Service Worker fetch方法中获取通过JS POST请求发送的参数

[英]How to get parameters sent through JS POST request in Service Worker fetch method

I'm pretty new in the all "Service Worker" thing (yeah I know, almost 2022 and I just get into it).我在所有“服务工作者”方面都很新(是的,我知道,快到 2022 年了,我才刚刚开始)。

I'm trying to use it to cache some requests'responses, but as you probably know it can't cache a POST request.我正在尝试使用它来cache一些请求的响应,但您可能知道它无法缓存 POST 请求。

Knowing this, I'll cache it in an other way but I want to be sure I have to cache it, and for this I need to know what is the value of the "data" parameter of the jQuery.ajax() method used in the origine of the fetch triggering.知道了这一点,我会以另一种方式缓存它,但我想确保我必须缓存它,为此我需要知道所使用的jQuery.ajax()方法的“data”参数的值是什么在获取触发的起源中。

I actually can't figure out how to retrieve the data parameter with the event object, nor the event.request one.我实际上无法弄清楚如何使用event对象或event.request检索数据参数。 So far I was able to do this :到目前为止,我能够做到这一点:

self.addEventListener('fetch', function(event) {
  const requestUrl = new URL(event.request.url);

  event.respondWith(
    caches.match(event.request)
    .then(function(response) {
        if(response) { console.log('return cache '+requestUrl.href); return response; }
        
        console.log('return classic + save');
        return fetch(event.request).then(function(response) {
            if(!response || response === undefined || response.status === undefined || response.status !== 200 || response.type !== 'basic') { return response; }
            
            var respToCache = response.clone();
            
            caches.open('thiscache').then(function(cache) {
                console.log('cache opened, saving...');
                cache.put(event.request, respToCache)
                .then()
                .catch(function(error) {
                    console.log('catcherror: '+error);
                    
                    if(event.request !== undefined && event.request.method !== undefined && event.request.method == 'POST') {
                        // Here, I need to get the 'data' parameter from the jQuery.ajax() method in the origin of the fetch
                    }
                })
            })
            .catch(function(error) {
                console.log('Error ! '+error);
            })
            
            return response;
        })
    })
  );
});

I can't find the right method to retrieve this data;我找不到检索这些数据的正确方法; thank you for your help and any tip you could have.感谢您的帮助和任何提示。

Edit:编辑:

For information, here's an example of a jQuery.ajax() that triggers the service worker's fetch :有关信息,以下是触发 Service Worker fetch 的 jQuery.ajax() 示例:

jQuery.ajax({
    url: './ajax.php',
    type: 'POST',
    dataType: 'html',
    xhrFields: { withCredentials: true },
    data: 'param1=value1&param2=value2...', // <-- this is what I want to get
    success: function(reponse, statut) { ... }
});

Found it !找到了 ! To perform that, you have to .formData() the event.request (a clone of it actually), that will give you all the parameters and their values contain in the data parameter.要执行此操作,您必须使用.formData() event.request (实际上是它的克隆),这将为您提供所有参数及其包含在data参数中的值。

self.addEventListener('fetch', function(event) {
  var requestClone = event.request.clone();

  event.respondWith(
    caches.match(event.request)
    .then(function(response) {
        if(response) { console.log('return cache '+requestUrl.href); return response; }
        
        console.log('return classic + save');
        return fetch(event.request).then(function(response) {
            if(!response || response === undefined || response.status === undefined || response.status !== 200 || response.type !== 'basic') { return response; }
            
            var respToCache = response.clone();
            
            caches.open('thiscache').then(function(cache) {
                console.log('cache opened, saving...');
                cache.put(event.request, respToCache)
                .then()
                .catch(function(error) {
                    console.log('catcherror: '+error);
                    
                    if(event.request !== undefined && event.request.method !== undefined && event.request.method == 'POST') {
                        requestClone.formData().then(formData => {
                          // Do whatever you want with formData, parameters are here :)
                        }
                    }
                })
            })
            .catch(function(error) {
                console.log('Error ! '+error);
            })
            
            return response;
        })
    })
  );
});

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

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