0

I'm trying to make a GET/POST request with fetch in service worker file, but it sends only OPTIONS request and not sending my original request. below is my code:

fetch('http://cross-origin-server/controller/function',{
    headers: {  
      "x-app-header": 'app'
    },
    "mod":"no-cors"
  }).then(function(response) {
    console.log(response);
  });

Here are the request and response headers :

Request Headers

    Accept:*/*
    Accept-Encoding:gzip, deflate
    Accept-Language:en-GB,en-US;q=0.9,en;q=0.8
    Access-Control-Request-Headers:x-custom-header
    Access-Control-Request-Method:GET
    Cache-Control:no-cache
    Connection:keep-alive
    Host:192.168.1.215
    Origin:http://localhost:3000

Response Header

    Access-Control-Allow-Headers:Content-Type, Content-Length, Accept-Encoding, Authorization,Cache-Control, x-custom-header, x-requested-with
    Access-Control-Allow-Methods:GET, OPTIONS
    Access-Control-Allow-Origin:*
    Cache-Control:no-store, no-cache, must-revalidate
    Connection:Keep-Alive
    Content-Length:21
    Content-Type:text/html; charset=UTF-8

I am not able to understand why my original request is not sending. I have allowed all origins from my server side (Access-Control-Allow-Origin:*). Please help, Thanks in advance

Edit: The problem is "real request is not sending" after preflight(OPITONS) request is complete.

Prince Gautam
  • 188
  • 12
  • Does your Console not give you any error messages? `*` is not an acceptable value for `Access-Control-Allow-Origin` when responding to a preflight request. – Quentin Oct 30 '17 at 07:34
  • Nope , it was not giving any error. I also changed the value of Access-Control-Allow-Origin to http://localhost:3000. But no change – Prince Gautam Oct 30 '17 at 07:39
  • Please @Quentin this question is not duplicate, please remove it from duplicate tag, I have also change the title of this question – Prince Gautam Oct 30 '17 at 09:24

1 Answers1

0

Service workers will only work with traffic served over https:// (with a limited exception for same-origin http://localhost requests). Your example shows that you're making a request against http://cross-origin-server, which, while obviously not a real URL, makes it seem like you're using http://.

Jeff Posnick
  • 45,379
  • 12
  • 113
  • 142