javascript - Request headers not sent from Service Worker -
i'm trying fetch web service service worker. service jsp secured basic apache authentication, must provide credentials authenticate in request headers. following request works fine main window:
self.addeventlistener('push', function(event) { console.log('received push message', event); event.waituntil( fetch(online_site_endpoint, { method: 'get', mode: 'cors', headers: { 'accept': 'application/json', 'authorization': 'basic btoa(auth info)' } }).then(function(response) { //process response }).catch(function(err) { }) ); });
that code event.waituntil() scope, function called 'push' event listener. however, same exact call fails 401 (unauthorized). network panel developer tools shows headers not being sent:
options /latest-new.jsp http/1.1 host: {an accessible host} connection: keep-alive access-control-request-method: origin: http://localhost user-agent: mozilla/5.0 (x11; linux i686) applewebkit/537.36 (khtml, gecko) chrome/43.0.2357.134 safari/537.36 access-control-request-headers: accept, authorization accept: */* referer: http://localhost/service-worker.js accept-encoding: gzip, deflate, sdch accept-language: en-us,en;q=0.8
is there missing here? or can't achieved service worker?
some info: can't use xmlhttprequest since 'not defined' on service worker scope. headers on jsp before retrieving json:
response.setheader("access-control-allow-origin", "*"); response.setheader("access-control-allow-methods", "post, get, options, delete"); response.setheader("access-control-max-age", "3600"); response.setheader("access-control-allow-headers", "x-requested-with");
update: there authentication headers service workers, since requests non-secured urls not fails. same service without apache authorization works expected.
you should set allowed headers accept , authorization
response.setheader( "access-control-allow-headers", "x-requested-with, accept, authorization" );
also body of response "options" request should empty (it not necessary indeed, there no use case body in such response) , content-length:
should 0 (zero)
please note, request should not passed application (you can, not need)
Comments
Post a Comment