Rails Can't verify CSRF token authenticity ajax with X-CSRF-TOKEN header
<p>I try to register a service worker endpoint in my database but when I send my post data with fetch the app raise an error.</p>

<p>I want to keep the csrf verification. Do you see something wrong ?</p>

<pre><code>            var ready;

            ready = function(){
              if ('serviceWorker' in navigator) {
               console.log('Service Worker is supported');
                navigator.serviceWorker.register('/service-worker.js').then(function(reg) {

                 reg.pushManager.subscribe({
                     userVisibleOnly: true
                 }).then(function(sub) {
                     console.log('endpoint:', sub.endpoint);
                     console.log(sub);
                     var token = $('meta[name=csrf-token]').attr('content')
            console.log(token);

                     return fetch('/register_endpoint', {
                    method: 'post',
                    headers: {
                      'Content-type': 'application/json',
                      'X-CSRF-TOKEN': token
                    },
                    body: JSON.stringify({
                      endpoint: sub.endpoint,
                      authenticity_token: token

                    })
                  });
                 });


               }).catch(function(err) {
                 console.log('Erreur -&gt; ', err);
               });
              }

            };



            $(document).ready(ready);
            $(document).on('page:load',ready);
</code></pre>

<p>thanks</p>